I have a base class that contains a structure definition using a custom data type. I am doing this so I can keep thing extensible through a common interface for DLC content later on.
class base
{
public enum testTypes;
struct data
{
testTypes field1;
}
public data testdata;
}
class dataset1
{
public enum testTypes = { MAMMAL, BIRD, FISH };
testdata = new data[]
{
MAMMAL,
BIRD
};
}
class dataset2
{
public enum testTypes = { CAR, TRAIN, BOAT };
testdata = new data[]
{
CAR,
BOAT
};
}
C# doesn’t seem to allow me to create different enums for different instances. Is there any way around this? Any trick or solution?
You can’t really do that; when you make the class “base” and give it an enum, it is declaring a new enum of type “base.testTypes”. Just because the three classes have the same name for their enum doesn’t mean it’s the same enum. The easiest way to do what you want is to just change base to use an int:
class base
{
struct data { int field1; }
public data testData;
}
and it should automatically convert the enums in the other classes back and forth into int. This is the easiest way to do it, not necessarily the best way to do it.
1 Like
You’ve really moved beyond what enums can do for you here. Enums are simply a wrapper for int. An enum is simply meant to make your code a little more human readable then simply handing around random numbers.
Another approach would be to use strings.
In addition to what others have said, you can use constants to define your int values.
public const int Mammal = 1;
public const int Bird = 2;
public const int Fish = 3;
public const int Car = 1;
public const int Train = 2;
public const int Boat = 3;
int animal = Bird;
And if you want to go all out you can kind of fake an enum with a class.
static class Animal
{
public const int Mammal = 1;
public const int Bird = 2;
public const int Fish = 3;
}
int animal = Animal.Bird;
2 Likes
Yeah, the int solution was always in the back of my mind, I just hoped I could avoid that because it is clumsy and defeats the purpose of enums. Nonetheless, it appears it is the only way for me to go, so…
Thanks for the replies.
What I’m wondering is what’s the purpose of the enums in your weird use case?
It’s really just for readability and less typing.
Now I changed the base class to take ints and in the derived classes I work with enums, which are then cast into ints. Not 100% as elegant as I had hoped but close enough.
It’s possible to do something similar in a couple ways:
Way 1: Make the base a generic class:
public class Inventory<E> {
public void AddItem ( E item ) {
_contents.Add(item);
}
public bool HasItem ( E item ) {
return _contents.Contains(item);
}
private List<E> _contents = new List<E>();
}
public enum MarioItems {
MUSHROOM,
FIRE_FLOWER,
LEAF
}
public class MarioInventory : Inventory<MarioItems> { }
Way 2: Store the enums as strings internally:
public class Inventory {
public void AddItem<E> ( E item ) {
_contents.Add(ConvertEnum(item));
}
public bool HasItem<E> ( E item ) {
return _contents.Contains(ConvertEnum(item));
}
private List<string> _contents = new List<string>();
private string ConvertEnum<E> ( E id ) {
return id.ToString() + "." + typeof(E).FullName;
}
}
...
var player1 = new Inventory();
player1.Add(MarioItems.MUSHROOM);