March 6, 2008

WPF Links

AsReadyOnly method

The AsReadyOnly method. This method is exposed by the List, Set, Array<>, and others and generates what is basically an adapter to your collection. The return value is a System.Collections.ObjectModel.ReadOnlyCollection that is the best of both worlds , it shows live data from the internal collection and it doesn't let the consumer modify the collection contents. Here's an example of the implementation
public class MyManagerClass
{
     private List _objects = new List();

     public ReadOnlyCollection Objects
     {
          get { return _objects.AsReadOnly(); }
     }
}

Setting up Log4Net in the application config file.

Setting up log4net to use a rolling file appender and a trace appender
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net debug="false">
    <appender name="RollingFileAppender"
 type="log4net.Appender.RollingFileAppender">
      <file
 value="${ENVIROMENTALVARIABLENAME}\\MyApplicationName.exe.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <maxSizeRollBackups value="5" />
      <param name="RollingStyle" value="Size" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -
 %m%n" />
      </layout>
    </appender>
    <appender name="TraceAppender"
 type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>
    <root>
      <priority value="ALL" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="TraceAppender" />
    </root>
  </log4net>
</configuration>

Reading The Number Of Rows In A Table Search

const string NUM_XXX = 
"SELECT COUNT(*) AS numXXXs"
"FROM ... 
"WHERE ...

using (databaseConnection = new SqlConnection(XXXConnectionString))
{
    databaseConnection.Open();
    using (SqlCommand command = new SqlCommand())
    {
        command.Connection = databaseConnection;
        command.CommandText = NUM_XXX;
        command.Parameters.AddWithValue("@Id", id);

        SqlDataReader reader = command.ExecuteReader();
        if (reader != null)
        {
            if (reader.HasRows && reader.Read())
            {
                numXXXs = reader.GetInt32(0);
            }
            reader.Close();
        }

    }
}

Simple Background Worker Object Template

BackgroundWorker backgroundWorker
Worker worker;

private void AsyncStartWork()
{
    if (!backgroundWorker.IsBusy)
    {
        worker = new Worker(...);
        backgroundWorker.DoWork += new
 DoWorkEventHandler(backgroundWorker_DoWork);
        backgroundWorker.RunWorkerCompleted += new
 RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        backgroundWorker.RunWorkerAsync((object)worker);
    }
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Worker worker = e.Argument as Worker;
    if (worker != null)
    {
        worker.DoSomething();
    }
}

private void backgroundWorker_RunWorkerCompleted(object sender,
 RunWorkerCompletedEventArgs e)
{
    backgroundWorker.DoWork -= new
 DoWorkEventHandler(backgroundWorker_DoWork);
    backgroundWorker.RunWorkerCompleted -= new
 RunWorkerCompletedEventHandler(
        backgroundWorker_RunWorkerCompleted);

    // Update GUI

    // IF necessary do something with results
    Show(worker.Results);
}

Links To Developer Tools

Web Developer Add In for FireFox
Yet another VB to C# Converter
Ultimate Developers And Power Tool Users List
Console A Command Prompt Replacement