C
cody
Why can't switch used for objects, at least for static readonly fields it
wold be nice.
The Jit could certainly optimize this case because the addresses of static
readonly variables are known at jit time.
The thing is that is often used my own enum classes because I usually want
to assign a userfriendly (and localizable) name to an enum member which is
automatically displayed if I add a enum member to a combobox for example.
Additionally I have helper methods for returning me a list of all or a
specific subsets of the member of the enums:
public class Color
{
public static reaonly Color Red = new Color(255,0,0);
public static reaonly Color Green = new Color(0,255,0);
// and so on
private Color(){}
public Color[] GetColors() ..
public Color[] GetGreenLikeColors() ..
public Color[] GetDarkColors() ..
public Color[] GetSystemColors() ..
Color GetByName(string name){}
Color GetByID(string name){}
}
The getXX methods work mainly using hashtables and the great thing is that
if using generics I could derive all enums from some base enum class without
having to add new getXX methods which return Color[] instead of object[].
The stupid thing is if I change some enum into my enum class I always have
to rewrite all switches into huge ugly if's.
I see no technical reason why switch shouldn' be allowed for objects.
Certainly is won't be as fast as with constant int values but anyway they
allowed strings in switch statements.
In a modern language like C# the purpose of a switch statement shouldn't be
to make a program faster but more readable.
Am I just plain stupid or is there something huge I overlooked?
wold be nice.
The Jit could certainly optimize this case because the addresses of static
readonly variables are known at jit time.
The thing is that is often used my own enum classes because I usually want
to assign a userfriendly (and localizable) name to an enum member which is
automatically displayed if I add a enum member to a combobox for example.
Additionally I have helper methods for returning me a list of all or a
specific subsets of the member of the enums:
public class Color
{
public static reaonly Color Red = new Color(255,0,0);
public static reaonly Color Green = new Color(0,255,0);
// and so on
private Color(){}
public Color[] GetColors() ..
public Color[] GetGreenLikeColors() ..
public Color[] GetDarkColors() ..
public Color[] GetSystemColors() ..
Color GetByName(string name){}
Color GetByID(string name){}
}
The getXX methods work mainly using hashtables and the great thing is that
if using generics I could derive all enums from some base enum class without
having to add new getXX methods which return Color[] instead of object[].
The stupid thing is if I change some enum into my enum class I always have
to rewrite all switches into huge ugly if's.
I see no technical reason why switch shouldn' be allowed for objects.
Certainly is won't be as fast as with constant int values but anyway they
allowed strings in switch statements.
In a modern language like C# the purpose of a switch statement shouldn't be
to make a program faster but more readable.
Am I just plain stupid or is there something huge I overlooked?