views:

184

answers:

6

As C# lacks support for freestanding functions, I find it hard to find a place to put conversion functions. For example, I'd like to convert an enum to a number. In C++, I would make the following freestanding function for this:

UINT32 ConvertToUint32(const MyEnum e);

How can I elegantly do this in C#? Should I make a dummy static class for holding the function, and if so, how can I find a meaningful name for it? Or should I make a partial class Convert?

Any ideas?

Thanks in advance.

Update: In retrospect, my example was not very well chosen, as there exists a default conversion between enum and int. This would be a better example:

Person ConvertToPerson(const SpecialPersonsEnum e);
+1  A: 

I would recomend that you create and assembly that would contain all your helper methods/constants and enums that will be used in other projects.

This will allow you to easily include this assembly with other assemblies that need it and avoid circular references.

astander
+9  A: 

The above example looks like a candidate for an Extension Method.
If that's not possible, I define them as static methods in a static class ; I'd normally put them in a static class called XXXHelper

Gishu
In this specific case a cast would be fine, in the general case I agree with Gishu and try to use extension methods before spawning a "helper" class. Keep the functionality as close to where it belongs as possible (whilst maintaining SRP of course).
Chris Meek
You're right! I didn't expect extension methods to work on enumerations, but apparently they do.
Dimitri C.
+1  A: 

Can't you just use a cast for this? (UInt32)e. Or else call Convert.ToUInt32(e)

Tim Goodman
Yes, but I'm talking about the general case. My example was merely to make the situation clear.
Dimitri C.
+1  A: 

Hi Dimitri,

The idea of creating a static "dummy class" seems to be what Microsoft suggests:

http://msdn.microsoft.com/en-us/library/bb383974.aspx

I think in your particular example doing a partial class Convert makes the most sense.

S.C. Madsen
+1  A: 

I faced the similar problem once and I did this

class Program
{
    static void Main(string[] args)
    {
        string s = "123";
        int n = Convert.StringToInt(s);
    }
}

class Convert
{

    public static int StringToInt(string s)
    {
        // implementation
    }
    public static string IntToString(int n)
    {
        // implementation
    }
}
TheMachineCharmer
for that ...why dint you use the .Net System.Convert Class?
SysAdmin
+8  A: 

I'd go with:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int ConvertToInt(this MyEnum e)
        {
            var m;

            // ... Implementation

            return m;
        }
    }   
}

Then you'd simply use MyEnum.ConvertToInt(); The same can be done for multiple conversions all from within the same class. Extension methods are in a nutshell, damn sexy.


Also, Eric's comment about Type Converters got me googling. Pretty awesome, however I'm not sure how to use them with an Enum, but for other conversions, they're clean as a whistle to implement. Have a look here:

Kyle Rozendo
Thanks for the example.
Dimitri C.