Use enum from another class?
How do I use enum values from one class in another?
I have this class:
Code:
public class class1
{
[FlagsAttribute]
public enum ContainterTypes : int
{
CT_OUTLOOK = 0,
CT_XML = 1,
CT_SDX = 2,
CT_SAP = 4
};
}
and am simply trying to set a variable inside of another class to one of these enumerated values:
Code:
blahblah.SetContainerType(CT_XML);
which obvously doesn't work... I have tried
Code:
blahblah.SetContainerType(class1.ContainterTypes.CT_XML);
but no luck there either.
I have set a reference to the first class in the second.
what am I missing here?
RESOLVED: Re: Use enum from another class?
Code:
blahblah.SetContainerType((int) class1.ContainterTypes.CT_XML);
works.
Just so I know, can someone explain why I have to cast to int since there is a reference to the class that contains the enumeration? I would think the cast would be unnecessary.
thanks.
Re: Use enum from another class?
In enumeration, the enumerators are also referenced by integer values 0 - the end enumerator, so:
Code:
enum MyEnum { Red, Blue, Green } Colors;
Blue can be assigned using the integer 1 just as Red = 0, Blue = 1, Green = 2, and so on.
Re: Use enum from another class?
What error are you getting? I assume you just coded your method incorrectly and it's throwing an error saying you can't convert from enum to int? Your method signature should look like this:
Code:
void SetContainerType(class1.ContainterTypes container)
{
if(container == class1.ContainerTypes.CT_XML)
ConvertToXml();
}
I also wouldn't recommend casting your enums to and from their values for internal code, for example this is bad:
Code:
// This is bad
switch((int) myEnum)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
// This is good
switch(myEnum)
{
case MyEnum.Red:
break;
case MyEnum.Blue:
break;
case MyEnum.Green:
break;
}
Re: Use enum from another class?
It looks like SetContainerType is expecting an int and not ContainterTypes as its parameter, so, as Mutant_Fruit suggested just change the parameter type from int to ContainerTypes and you won't need to cast.
Also you should consider using singular for the enum type name, e.g., ContainerType instead of ContainerTypes, http://msdn2.microsoft.com/en-us/lib...01(VS.71).aspx
Why is this so damned obtuse?
In C++, I have a nice little header file with 4 #defines...
All I need to do is #include the header.. then I can use the defines all over the place.. all I want to do is use some english-looking text instead of 0, 1, 2, etc.
But in C# I gotta reference it with World. Namespace.File.Class.Somethingelse.gibberish.yadayadayad
why is this so much more difficult???
Re: RESOLVED: Re: Use enum from another class?
Quote:
Originally Posted by purpleflash
....can someone explain why I have to cast to int since there is a reference to the class that contains the enumeration? I would think the cast would be unnecessary.....
When your SetContainerType is like
Code:
public void SetContainerType(int type){
....
}
Then you need to cast. as you are awaiting an integer here
if instead of this you have
Code:
public void SetContainerType(ContainerTypes type){
...
}
then this works without troubles. May I ask why you have it this way ?
Code:
public class class1
{
[FlagsAttribute]
public enum ContainerTypes : int
{
CT_OUTLOOK = 0,
CT_XML = 1,
CT_SDX = 2,
CT_SAP = 4
};
}
Instead of this
Code:
public enum ContainerTypes : int
{
CT_OUTLOOK = 0,
CT_XML = 1,
CT_SDX = 2,
CT_SAP = 4
};
public class class1 {
...whatever needs using your enum here
}
as the enum is public its quite useless to put it into the class, isn't it ? And use singular names for enum as you in every case have one of them.
Re: Use enum from another class?
Ok, that makes sense.. I'll simplify it as you have suggested.
Re: Why is this so damned obtuse?
Quote:
Originally Posted by purpleflash
In C++, I have a nice little header file with 4 #defines...
All I need to do is #include the header.. then I can use the defines all over the place.. all I want to do is use some english-looking text instead of 0, 1, 2, etc.
But in C# I gotta reference it with World. Namespace.File.Class.Somethingelse.gibberish.yadayadayad
why is this so much more difficult???
I know you have this solved already using JonnyPoet's suggestion of the public enum, but I thought I mention a couple of things.
In C++, your 4 #defined wasn't type safe. You had to defined the #define values and then call them in a function with
Code:
void SomeFn( int pseudoEnum );
As such you had to explicitly write the range validation. In C# you get that for free (because you can't ever use a valid that isn't in the enum).
With regard to Namespace.File.Class.Somethingelse.gibberish.yadayadayad
You don't need to always specify the fully qualified name of the object (and I'm sure you've already figured it out). You have 3 options:
1) Fully qualified name
2) If the enum (const, class, etc) is in the same namespace as where you are calling it from, just call the enum (const, class, etc) directly (no ns needed)
3) Use the 'using' statement to include the namespace in the file (if the enum or other item is in a different namespace).
Re: Use enum from another class?
Create an empty code file and give it a namespace and create a public enum without a class.
Then you can utilize the namespace reference and call the public enum from any class that your namespace is referenced.
Enums do not need to be in class files.
In fact I use public enums all the in my business entities layer.
Re: Use enum from another class?
Quote:
Originally Posted by ntfishersdk
Create an empty code file and give it a namespace and create a public enum without a class.
Then you can utilize the namespace reference and call the public enum from any class that your namespace is referenced.
Enums do not need to be in class files.
In fact I use public enums all the in my business entities layer.
Yep, thats what I basically mentioned in post #7 to get the enums out of his classes on top of the (any ) namespace. :thumb: