views:

51

answers:

2

I have a Direction Enum:

Public Enum Direction
    Left
    Right
    Top
    Bottom
End Enum

And Sometimes I need to get the inverse, so it seems nice to write:

SomeDirection.Inverse()

But I can't put a method on an enum! However, I can add an Extension Method (VS2008+) to it.

In VB, Extension Methods must be inside Modules. I really don't like modules that much, and I'm trying to write a (moderately) simple class that I can share in a single file to be plugged into other projects.

Modules can only reside in the file/namespace level so I have one in the bottom of the file now:

Public Class MyClass
    '...'
End Class

Public Module Extensions
    <Extension()> _
    Public Function Inverse(ByVal dir As Direction) As Direction
        '...'
    End Function
End Module

It works, and "if it ain't broke, don't fix it", but I'd love to know if I'm doing it wrong and there's a better way with less boilerplate. Maybe in .NET 4?

Finally, I know I could write a structure that behaves like an enum, but that seems even more backwards.

+4  A: 

That is indeed the only way to add (the appearance of) a method to an enum.

Note that in C# the extension method would be defined on a static class, but that is a trivial difference, largely one of nomenclature.

Marc Gravell
in VB a class can't be declared shared (static). AFAIK they're equivalent to modules. But since an extension method is just sintactic sugar for a static one, I don't understand the need to put it in a different class (since a static method can reside in a non-static class)
Camilo Martin
An *extension* method cannot (in C#) reside in a non-static class, note.
Marc Gravell
I know it can't, but I don't understand the reason behind this.
Camilo Martin
+1  A: 

You can make a class that behaves like an enum, in the sense that it has static readonly properties that return an instance of the class that contains that value.

The System.Drawing.Color structure for example works this way (although the internal value is not an enum); it has static properties like Color.Redand Color.White, as well as instance properties and methods.

Guffa
Well, that would be way more boilerplate than just a module. Even a class is not necessary to emulate an enum - the same could be done in a structure. But it really looks weird to see a structure pretending to be an enum.
Camilo Martin
@Camilo Martin: Yes, as structure can also be used, but the reason that I suggested a class is that a structure is more complicated to implement correctly. It may seem weird to use a class intead of an enum, but on the other hand you get the methods in the class itself instead of in a separate module.
Guffa
Why complicated? This answer's code looks simple: http://stackoverflow.com/questions/1395401/enum-extension-method-todatasource/1395439#1395439
Camilo Martin
@Camilo Martin: Well, that's an example why structures are more complicated to implement; that code doesn't compile. When fixed, it still doesn't support things that you take for granted, like the `==` operator. If you change it to a class, it works just fine (with only changing the private static properties to public).
Guffa
Ah, I see. Thanks :)
Camilo Martin