Passing enumerator as generic type

J

Jon Slaughter

I created a generic class and I want to pass it a enum,


class GObject<SomeType>
{

...
public SomeType q = SomeType.Default;
...
}

enum MyEnum
{
....
Default = x;

}

Then later,

GObject<MyEnum> T;


The type passed to GObject should always contain a default value and I need
to have GObject use it to set some initial values of fields. I could, of
course just set everything to 0 but it may turn out that the default value
shouldn't be 0.

Its not a huge issue cause I can just force the default value to be 0 but
this method seems more elegant.

I guess C# isn't going to do this though because SomeType is a generic
object and I'm trying to restrict it to a smaller class?

Thanks,
Jon
 
J

Jon Slaughter

An example class might be


public class GObject<ImageSelection>
{
public PointF Loc = new PointF(0, 0);

Dictionary<ImageSelection, Image> Images = new
Dictionary<ImageSelection, Image>(1);

ImageSelection LastImage;
ImageSelection currentImage;
public ImageSelection CurrentImage
{
get { return currentImage; }
set { LastImage = currentImage; currentImage = value; }
}

public GObject(ImageSelection x) { CurrentImage = x; }
public void AddImage(ImageSelection sel, Image image) { Images[sel] =
image; }
public Image GetImage() { return Images[CurrentImage]; }
}

which basically makes a correspondence between an enum representing the
states of an image and the image information. (so the above code
encapsulates an image list)

I'll end up adding some other code such as mouse testing and stuff later,

The main issue here is that the constructure

public GObject(ImageSelection x) { CurrentImage = x; }

is requires to initialize the default values but its unecessary,

enum MyImages
{
Red,
Black,
Brown,
Default = Red
}


and I'd like to use the default as initialization so I would have

public GObject() { CurrentImage = ImageSelection.Default; }

Since ImageSelection turns in MyImages it should work? But the compiler is
unfortunately treating it like a generic object. Maybe I can use reflection
to get the value of the enum? (I'd like a compile time solution if possible.

Thanks,
Jon
 
B

Ben Voigt [C++ MVP]

Since ImageSelection turns in MyImages it should work? But the compiler is
unfortunately treating it like a generic object. Maybe I can use
reflection to get the value of the enum? (I'd like a compile time solution
if possible.

Since you can't have a static interface in .NET (enum constants are static
members), you can't do this compile-time.

If you use reflection you probably want to cache the result, then your
performance hit won't be so bad and you can regain some type safety by
encapsulating the casts.

something like:

static class DefaultFor<TEnum>
{
private static initonly TEnum defvalue;
static DefaultFor()
{
// extra error-checking might be good
defvalue = (TEnum) typeof(TEnum).GetFields("Default",
MemberBinding.Static | MemberBinding.Public)[0].GetValue(null);
}
public static TEnum Value { get { return defvalue; } }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top