- Home /
Cast the result of an enum as a string
Hi everyone,
I've got this function working as an Instantiate, meaning that it needs a string, a position and a rotation. Using a system I made, I can implement the position and the rotation, but I've got troubles with the string.
I need to call on something using the result of an enum. So I've got this enum:
public enum enumType {ONE, TWO, THREE, FOUR};
public enumType type;
But since it's giving me an int, I cannot convert this to a string. The enum is important because it allows me to set things appropriately.
Is there any way that I could "interpret" the result of the enum so that I could send a correct string to my function?
NOTE : I'm coding in c#.
??? Instantiate uses GameObjects or Transforms -- they don't need strings. And the purpose of an enumerated type is to make a working program easier to read, by turning 1,2,3 into meaningful words, like COW, DOG, LION.
I think you're halfway down a bad road to solve whatever your original problem was.
I understand your comment, and I want to say that you're not wrong. It's just that I'm using a singleton that instantiates all my prefab at the beginning of the game. To get them back, I need to use a string to get them, but I have this enum problem. See what I mean?
Answer by GutoThomas · Apr 22, 2012 at 03:27 PM
Have you already tried explicit conversion?
Like
(string)type
or
type as string
?
In my case, if I ain't wrong, the result is always passed like some sort of string. It only works as an int if a do an explicit conversion, like the one I show before.
And like any type in .NET you could also use the ToString function explicitly or implicitly:
public enumType type = enumType.TWO;
string enemy = type.ToString(); // explicit
// or
string enemy = "" + type; // implicit
(string)type
&&
type as string
did not work, but .ToString() did!
Answer by AchillesWF · Apr 22, 2012 at 03:25 PM
Well, interestingly in your enum ONE is actually zero, TWO is one, and so on. So even if you just used .ToString() on the int representation of the enumeration value, you will not technically get the correct results. To change this, make your enum like this:
public enum enumType { ONE = 1, TWO, THREE, FOUR }
int values should have a ToString() function available in C# that gives you the string representation of the number.
Answer by Herve_Simard · Apr 23, 2012 at 04:51 PM
I have my answer (finally!).
Here:
string enemy = Enum.GetName(typeof(Wave.enemyType), waves[currentWave].type); where Wave.enemyType is my enum, and waves.type is the variable of my enum.
That's a bit complicated to just get the enums value as string. José Augusto $$anonymous$$ showed you an easier way.
btw. It's common practise to start type names with a capital letter: "EnumType" so they don't get confused with valiable names like "currentWave".
Your answer
Follow this Question
Related Questions
How to convert a list of Resolution to String 2 Answers
What is this C# code in javascript 0 Answers
Most efficient way to convert string[] to int [] in C#? 2 Answers
Convert Text to float 3 Answers
Convert Int to String? 1 Answer