No IsInteger() in C#?

Filed Under (development) by sith on 22-09-2008

Tagged Under : ,

(* this is a legacy post from one of my earlier blogging attempts, better formatting to come)

Unless I just can’t find it, there doesn”t seem to be a method to test a value for whether it is a valid integer (or at least could be turned into a valid integer) in C#. For example, today I needed a way to test a string value before converting it to an integer. The original code looked something like this:

int ConvertedNumber = Convert.ToInt32(TextBox1.Text);

Of course, we all see major issues with this line of code. What if TextBox1 doesn’t contain a number? What if TextBox1 doesn’t contain anything at all? It wouldn’t have been so bad if there was some protection around this, but there wasn’t. So…the need to write an IsInteger() method kicked in, and here’s what we came up with.

private static Regex _isNumber = new Regex(@"^\\d+$");

public static bool IsInteger(string theValue)
{
    bool result = false;

    Match m = _isNumber.Match(theValue);
    return m.Success;
}

During my initial testing, this looked like it was going to work perfectly, until I found the following scenario…what if a user typed in “01″? They would be expecting this to be converted to a “1″. Unfortunately, my regular expression couldn”t handle it, so the following hack is what we came up with:

public static bool IsInteger(string theValue)
{
    bool result = false;

    Match m = _isNumber.Match(theValue);
    result = m.Success;

    if (!result)
    {
        try
        {
            int ConvertedNumber = Convert.ToInt32(theValue);
            result = true;
        }
        catch
        {
            result = false;
        }
    }
    return result;
}

I really don’t like using try…catch to handle normal code flow, but I’m still learning regular expressions, and couldn’t figure out how to handle this scenario. If anybody out there wants to help me out, that would be appreciated. If I end up figuring it out myself, I’ll update this article.

Maybe this is a good time to start a common methods assembly. :)

How to create a COM Interop DLL in C# (simple example)

Filed Under (development) by sith on 21-09-2008

Tagged Under : , ,

(* this is a legacy post from one of my earlier blogging attempts, better formatting to come)

Well, it finally happened. I had to force myself to figure out how the fsck to call a method in a C# assembly from a Delphi 5 Win32 application. The following is the culmination of all of the buttons that I clicked and swearing that I sweared…

And now our feature presentation.

1. Create a new Class Library project in Visual Studio 2005. Let’’s call it “InteropExample”.
2. Open the AssemblyInfo.cs file and set the COM visible attribute to true.

[assembly: ComVisible(true)]

3. Go to Project Properties –> Build. Check the option Register for Com Interop to “Selected”.
4. Open your class file and add the foloowing to the using clause

using System.Runtime.InteropServices;

5. In this class file, create the interface that will help expose methods to the unmanaged world.

public interface IMathFunctions
{
    int Add(int Number1, int Number2);
}

6. Create the method that will be called.

[ClassInterface(ClassInterfaceType.None)]
public class ComInteropExample : IMathFunctions
{
    public int Add(int Number1, int Number2)
    {
        try
        {
            return Number1 + Number2;
        }
        catch
        {
            return 0;
        }
    }
}

7. Now it’’s time to create the type library that we will use in Delphi (or any other unmanaged language). This type library can be used for late binding. In our example, the assembly will be private, so copy the assembly into the folder where the Delphi compiled executable will be (they need to live in the same folder). If this assembly is going to be public, you need to assign a strong key to the assembly. I”ll cover that in another tutorial (when I learn more about it myself…hahahahah).

Open a command prompt and type the following:

regasm InteropExample.dll /tlb:InteropExample.tlb

8. Switch over to Delphi, create or open a project and click Project –> Import Type Library. Select your InteropExample.tlb and click Create Unit. The pascal file that is created can now be used in your program to call the managed methods.
9. In your main unit, add “ComObj” and “InteropExample_TLB” (or whatever your generated unit is called) to your uses list.
10. Now we can slap some code in to run this bad boy

procedure TForm1.Button1Click(Sender: TObject);
var
  intfMath: IMathFunctions;
  result: Integer;
begin
  intfMath := CreateComObject(CLASS_InteropExample) as IMathFunctions;
  result := intfMath.Add(2, 2);
  ...
end;

* CLASS_InteropExample might not be the actual name, review the pascal file that was created in Step 8

11. THE MOMENT OF TRUTH…

Run your Delphi app, click the button and watch the magic…

12. When deploying on another machine, you will probably have to register the C# assembly. Make sure that the assembly is living in the same folder as the Delphi executable, then from a command line

regasm InteropExample.dll

Enjoy.

NUnit and me

Filed Under (development) by sith on 21-09-2008

Tagged Under : , , , ,

So begins my latest foray into unit testing. Don’t ask why, but watching Conan The Barbarian has somehow inspired me to actually work at this a bit and finally try to figure out why/how to unit test.

I’m armed with a copy of Pragmatic Unit Testing in C# with Nunit, and a fresh attitude…so, here I go…

…after Conan is over. Oh yeah, if I do figure this out, maybe I’ll try putting together some tutorials that might help other monkeys out.