|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Passing enumerator as generic typeclass 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 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 > Since ImageSelection turns in MyImages it should work? But the compiler is Since you can't have a static interface in .NET (enum constants are static > 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. 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; } } } Now you can refer to DefaultFor<TEnum>.Value instead of TEnum.Default, a little more clunky but not bad. Show quote > > Thanks, > Jon > > |
|||||||||||||||||||||||