Putting enum text value in a string

Started by mode7, Wed 06/04/2011 20:29:34

Previous topic - Next topic

mode7

I'm trying to display an enum at a label. But i can only do so with the integer values.
Is there a possibility to show the enum text instead of the int value?
lstate.Text = String.Format("State: %d",CurrentState); //this i have now

lstate.Text = String.Format("State: %s",CurrentState); //this won't work

monkey0506

Unfortunately there's no direct way in AGS to do this, but you could define a custom specialized function to do it for you:

Code: ags
String GetEnumName(EnumType value)
{
  if (value == eValue1) return "Value1";
  if (value == eValue2) return "Value2";
  if (value == eValue3) return "Value3";
}

// ...
lstate.Text = String.Format("State: %s", GetEnumName(CurrentState));

Sephiroth

#2
*Edited:

Sorry if this is obvious but, why not use a simple string:

Code: ags

string CurrentState;

CurrentState = "Poisoned";
Label.Text = CurrentState;


Quote
show the enum text instead of the int value.

What you're referring to when you say 'text', is actually like a (int) variable.

Code: ags

enum Days {monday, tuesday, wednesday};

Days CurrentDay;
CurrentDay = monday;

//is the same as:

CurrentDay = 0;


mode7

Ok thanks a lot guys!
Yeah I know its like an int, I just thought there might be an easy way to do this.
Well a specialized function is way too complicated and I want to use the enum for convinience reasons.
So I'll just have to note down the numbers instead.

monkey0506

I don't see how "not[ing] down the numbers" is a simpler solution than the one I presented (or indeed why the one I suggested is "complicated" at all)..perhaps if you're only doing this for debugging purposes, that would make a bit more sense..but in any case, at least you got a solution for yourself. ;)

mode7

I'm just doing this for debugging reasons, yes. So no reason to blow up the code any further. I'd also have to edit this function everytime i add a state - so this is not really practical.
-thanks anyway monkey.

SMF spam blocked by CleanTalk