473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Passing enumerator as generic type

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<MyEnumT;
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
Nov 28 '07 #1
2 1762
An example class might be
public class GObject<ImageSelection>
{
public PointF Loc = new PointF(0, 0);

Dictionary<ImageSelection, ImageImages = 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
Nov 28 '07 #2
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; } }
}

Now you can refer to DefaultFor<TEnum>.Value instead of TEnum.Default, a
little more clunky but not bad.
>
Thanks,
Jon


Nov 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cool Guy | last post by:
I don't understand the third paragraph under the heading 'Generic type instantiations' on <http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>: | The .NET Common Language Runtime...
8
by: Cool Guy | last post by:
From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>: | As with the previous use of the Stack<T> class created with the Order | type, another instance of the specialized...
1
by: A. Burch | last post by:
What is wrong with this type of generic... I get this error.... Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.Queue<TMQ.TheRec>' is less accessible than...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
1
by: DAnDA | last post by:
i got these errors when i want to compile my project : private List<CIF.Business.Northwind.OrdersItem> ConvertListToBusiness (List<CIF.Data.Northwind.OrdersItem> list) {...
1
by: Hoss | last post by:
Discovered this today after alot of head-scratching, just thought I would toss it out into cyberspace in hopes that it would help someone. This of course, will not compile public class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.