Friday, September 21, 2007

Removing Non-Numeric Characters from a String

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());
 }

#     Comments [0]  
kick it on DotNetKicks.com
 Saturday, September 15, 2007

Sweet Sweet NUnit on VS2005 Action

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.

#     Comments [0]  
kick it on DotNetKicks.com
 Friday, September 14, 2007

Singleton Pattern

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

#     Comments [3]  
kick it on DotNetKicks.com
 Thursday, September 13, 2007

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

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 .csproj file in note pad and edit the items 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. It might work if you just delete the code behind and then rebuild, but I didnt't try this.

 

#     Comments [0]  
kick it on DotNetKicks.com
 Wednesday, September 12, 2007

DataTable Select Method

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.
EmployeeDataSet.PeopleRow[] 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

#     Comments [0]  
kick it on DotNetKicks.com