Type cast a enum to different enum type in the entire List; C#
-
public enum Type
{
None = 0,
Found =1,
NotAval = 2};
public enum ShortType
{
None = 0,
Fd =1,
NA = 2
};class Properties
{
public Type type;
};List props;
//After that List is populated
...
...
//Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
//Please advice//
-
public enum Type
{
None = 0,
Found =1,
NotAval = 2};
public enum ShortType
{
None = 0,
Fd =1,
NA = 2
};class Properties
{
public Type type;
};List props;
//After that List is populated
...
...
//Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
//Please advice//
Can you avoid that situation in the first place? Cast immediately while populating that list, for example. Or cast when getting the items out of the list.
ptr_Electron wrote:
Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
Not happening. At least, not really. Someone will probably suggest LINQ's Cast<T>[^], but all that does is change a normal loop into a lazy loop.
-
public enum Type
{
None = 0,
Found =1,
NotAval = 2};
public enum ShortType
{
None = 0,
Fd =1,
NA = 2
};class Properties
{
public Type type;
};List props;
//After that List is populated
...
...
//Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
//Please advice//
Go back and fix the problem. If you want long and short names you might be interested in a DescriptionAttribute.
-
Can you avoid that situation in the first place? Cast immediately while populating that list, for example. Or cast when getting the items out of the list.
ptr_Electron wrote:
Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
Not happening. At least, not really. Someone will probably suggest LINQ's Cast<T>[^], but all that does is change a normal loop into a lazy loop.
Thanks a lot
-
Go back and fix the problem. If you want long and short names you might be interested in a DescriptionAttribute.
Sure, Thanks a lot
-
public enum Type
{
None = 0,
Found =1,
NotAval = 2};
public enum ShortType
{
None = 0,
Fd =1,
NA = 2
};class Properties
{
public Type type;
};List props;
//After that List is populated
...
...
//Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
//Please advice//
Even if you could cast a List<T> to a different type, (which you can't without playing silly buggers a lot) you couldn't do what you want: the list is not of the enum, it is a List of the Class containing the enum, which is a very, very different item indeed. Summing you want to do this to have human readable names for your enum elements, have a look at this: Human readable strings for enum elements[^]
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
public enum Type
{
None = 0,
Found =1,
NotAval = 2};
public enum ShortType
{
None = 0,
Fd =1,
NA = 2
};class Properties
{
public Type type;
};List props;
//After that List is populated
...
...
//Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
//Please advice//
If the enums are in your sources, try the solutions already suggested. But if you cannot change the sources (e.g. Third Party), you may add a wrapper class - thus actually introducing a third enum which you use in your application, and whenever you need to communicate with Third Party, the wrapper does the translation. Not a nice solution, but feasable.