Post Seattle Code Camp

01.28.08

I spent my weekend attending Seattle code camp sessions. I learned a lot, but one of the biggest things I learned is how much I still don't know. My experience as a developer is limited I have yet to reach 2 years developing professionally and I am still in school currently working on my last quarter for my BS. I like to think that I am a good developer but speaking to people at the code camp is a nice ego check and I still have a long way to go before I can reach the status of elite alpha geek. My goals as a developer this year is to really dive into WCF and to study the code put out by the Patterns & Practices guys at Microsoft, specifically the Enterprise Library and Smart Client. What I really need is a chip I can insert behind my ear and just download the knowledge and experience I need, I cant seem to consume information fast enough otherwise.

One thing I would like to know is the process of how other people obtain deep knowledge of a subject not just an overview.  I am open to opinions please give me your 2 cents by Email Aaron@thesprage.com or leave a comment.

Seattle Code Camp

01.15.08

Seattle Code Camp on Jan. 26th from what I hear it is a little smaller than most but seems to have some good content. The schedule includes speakers such as William (Bill) Vaughn and Phil Haack. Most of the tracks covered will pertain to .Net but there are a few Java sessions as well.

The following sessions look interesting to me:
26th
Rethinking Unit Testing: xUnit.net
Data Enhancements in Visual Studio 2008
Windows Application Enhancements in Visual Studio 2008
Visual Studio Reporting
27th
An Introduction to Windows Communication Foundation
Black Belt DSA, building Duplex Agents
The Web via WCF
LINQ Overview

using Statement

01.10.08

The using Statement is simply syntax sugar for a try finally block. If you instantiate an object that implements IDisposable you can make your code look cleaner with the key word using.
Example:
StreamWriter sw = File.CreateText(filePath)
try
{
  sw.Write(someText);
}
finally
{
  sw.Dispose();
}
The above block works the same as:
using(StreamWriter sw = File.CreateText(filePath))
{
  sw.Write(someText);
}
More Info:
MSDN

System.Diagnostics.Debugger

10.11.07

Problem: You need functionality turned off or on when you are running your program in Debug mode.

Solution: System.Diagnostics.Debugger.IsAttached; it is a static property that returns true if there is a Debugger attached. I like to use this when debugging websevices, timing out while fixing a problem can be annoying.

if (Debugger.IsAttached)
     _webService.Timeout = -1;

More info:

MSDN

 

Removing Non-Numeric Characters from a String

09.21.07

Problem: I have an editable grid that needs to contain numeric integer values only.

My Solution: TryParse, this function accepts a string and an out value then returns a  bool True if it was successful, also it does not throw exceptions. I am a 100% sure there is someone with a better way of doing this but this is what I came up with. Feedback encouraged!

private string RemoveNonNumericValues(string p)
{
     int tempValue = 0;
     StringBuilder value = new StringBuilder(p.Length);
     foreach (char charValue in p.ToCharArray())
     {
         if (int.TryParse(charValue.ToString(), out tempValue))
             value.Append(tempValue.ToString());
     }
     return value.ToString();
}
private void dataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dataGridView[e.ColumnIndex, e.RowIndex].Value = RemoveNonNumericValues(dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString());
}

Sweet Sweet NUnit on VS2005 Action

09.15.07

This is great! I dont know why I didnt do this earlier, but you can Debug your code and run NUnit at the same time. You just need to set the Debug>Start Action to start external aplication NUnit.exe. Then set your start project to be the same project as your test code and tada you now have debuging and break point goodness wile running nunit.

 

Singleton Pattern

09.14.07

The singleton pattern in C# is very simple to implement and very useful. This pattern ensures that there is only one instance of its self providing a global point of access. There are different ways of doing this but I like to use the example below  because it is thread safe and clean.

public class User
{
     private int _userId = 1;
     private static User _instance = new User();
     static User()
     {
     }
     private User()
     {
     }
     public int UserId
     {
         get
         {
             return _userId ;
         }
         set
         {
             _userId = value;
         }
     }
     public static User Instance
     {
         get
         {
             return _instance;
         }
     }
}

Here is a quick test I wrote in NUnit that shows how it is used and comfirms there is only one instance.

[Test]
public void TestSingleton()
{
     User sngle1 = User.Instance;
     sngle1.UserId = 1;
     User sngle2 = User.Instance;
     sngle2.UserId = 2;
     Assert.AreEqual(sngle2.UserId, sngle1.UserId);
}

More Info:
Everything you need to know about C# singleton.
More Patterns

Unable to find the report in the manifest resources. please build the project, and try again.

09.13.07

I was reorganizing a project and I moved a crystal report after that I received this error at runtime "Unable to find the report in the manifest resources. please build the project, and try again." No amount of building could fix this problem. I eventually had to open up the project file in note pad and edit the entries in the xml/MSBUILD.

Here is what a Crystal Report should look like in your MSBUILD script.

<Compile Include="CrystalReport1.cs">
    <AutoGen>True</AutoGen>
    <DesignTime>True</DesignTime>
    <DependentUpon>CrystalReport1.rpt</DependentUpon>
    <SubType>Component</SubType>
</Compile>
<EmbeddedResource Include="CrystalReport1.rpt">
    <Generator>CrystalDecisions.VSDesigner.CodeGen.ReportCodeGenerator</Generator>
    <LastGenOutput>CrystalReport1.cs</LastGenOutput>
</EmbeddedResource>

After you correct the entry Rename the file Build then rename it again to the old name and build again.

DataTable Select Method

09.12.07

The Select method is really handy for filtering and sorting the rows you need from a strongly typed dataTable. It has 3 overloads Select() for returning all the rows as an array select(stringFilter) for returning the rows with out sorting and Select(stringFilter,stringSort) for returning rows with sorting. Here is quick way to fire everyone with the first name of bob in your employee management application.

var people = peopleDataTable.Select(
                                    "FirstName = bob AND Fired = 0",
                                    "LastName"
                                    ) as EmployeeDataSet.PeopleRow[];
foreach (EmployeeDataSet.PeopleRow person in people)
{
     person.Fired = true;
}


more Info:
MSDN
even better example

C#

Code Search Engine

08.23.07

Found a cool code search engine called Koders.com that crawls open source projects by language for code examples. It has a great syntax highlighting and plugs into your IDE.