- Home /
Logic and datatype existential problem
The title might sound scary but I have just a question about data types.
Quite simply, I would need something like this:
public class myclass {
int myint = 0;
string mystring = "";
public myclass(int localint, string localstring) {
myint = localint;
mystring = localstring;
}
}
public enum myenum : myclass {
enum1 = new myclass(9,"abr"),
enum2 = new myclass(99,"acad"),
enum3 = new myclass(999,"abra")
}
So that elsewhere, when I need 'abra', instead of manually instantiating it, and having countless duplicates all over the code, I just
myenum mylocalenum;
mylocalenum = enum3; //no mistake, the underlying class variables are predefined
The purpose is to have a selectable, pre-set 'myenum' which basically encapsulates another data structure which I predefine in the declaration phase.
This is because I have several data pre-sets by design, and I need to interact with them as with an enum (get their number, their descriptions, and basically associate them with predefined values).
If you have a solution, or even a resembling alternative, please let me know.
Answer by kromenak · Oct 08, 2011 at 07:45 AM
Couldn't you use inheritance and polymorphism for this sort of thing? Unless I'm misunderstanding, you want to be able to assign a value to a variable that represents a class with different values. From your example above, each enum essentially just represents a pair of numbers. You could just have a base class with some abstract functions which return data in the child classes.
But I guess I could understand resisting that route - lots of overhead, extra files - no good. I guess you could do something like this:
public class MyClass {
...
}
public enum MyEnum {
class1 = 1,
class2 = 2,
class3 = 3
}
public class OtherClass {
MyClass[] classes;
public void Awake() {
classes = new MyClass[3];
classes[class1] = new MyClass(99, "asda");
....
}
public MyClass GetStuff(MyEnum type)
{
return classes[type];
}
}
But it still feels kind of messy. I'd probably still go with inheritance. You simply can't use an enum in the way you are describing.
What an horrible shortco$$anonymous$$g for c#. looks like java can do this easily ins$$anonymous$$d. Being that we don't have typedefs even, what I need can't see$$anonymous$$gly be achieved in design time.
Answer by kenruze · Oct 14, 2011 at 03:17 PM
Add a constructor to your class that takes that enum as the only parameter and then have the constructor switch on the type to set the values.
public class myclass {
int myint = 0;
string mystring = "";
public myclass(int localint, string localstring) {
myint = localint;
mystring = localstring;
}
public myclass(myenum id)
{
switch(id)
{
case enum1:
myint = 9;
mystring = "abr";
break;
case enum2:
myint = 99;
mystring = "acad";
break;
case enum3:
myint = 999;
mystring = "abra";
break;
}
}
public enum myenum : myclass {
enum1,
enum2,
enum3
}
Your answer
Follow this Question
Related Questions
PropertyDrawer: Enum to select extended class 0 Answers
Custom AudioReverbPreset 0 Answers
custom class serialization problem 1 Answer
Enum style variable 2 Answers