[Explanation needed] Completely lost with enum States code

Hello! I understund how enum word itself. So in
private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
we build Enumeration list. Okey.

But how this connected with next part of code:

private States myState;
We create myState var of States type? But States IS Enumerator. Sorry, I feel like I have no clue about this part of code and this part:

myState = State.cell;
is also look incomprehensible for me. Can anyone explaine this pasrt for me step-by-step?

p.s. I explore this topic [SOLVED] Understanding Question about private enum State {...} & private State myState; but still don’t did not attain enlightenment

p.p.s. I add Debug.Log(myState); to Start(), to see result in console, and myState was = “cell”, 1st element in Enum. But why?

Hello @DeusEx,

private States myState;
We create myState var of States type? But States IS Enumerator.

Breaking it down;

  • private - this is the access modifier, this restricts access to the variable from other classes (code)
  • States - this is the Type as you correctly stated, so yes, the Enum
  • myState - this is a local variable, of Type States, but it has not been initialised yet, as such it doesn’t hold a value

This is similar to stating this;

private string myMessageForDeusEx;

  • private - this is again the access modifier
  • string - this is the Type of variable we are declaring
  • myMessageForDeusEx - is the name of the variable but currently doesn’t hold any text

myMessageForDeusEx = "Hello DeusEx";

now it does :slight_smile:

This is the same for your state, we will now set your myState variable;

myState = States.cell;

  • myState - this is your variable of Type States, and is set following the equals sign
  • States - this is the Enum, it contains all of the possible values
  • cell - this is one of the values from your Enum, which we are now using to set the value of myState

Chances are, as you were typing, after you pressed the fullstop after States a list of your options would have been displayed, this is because you had set them at the top of your script when you initialised your enum,

p.p.s. I add Debug.Log(myState); to Start(), to see result in console, and myState was = “cell”, 1st element in Enum. But why?

You haven’t provided your full script, but, if myState is outputting the value “cell”, then you must have set it prior to outputting it’s value using the Debug.Log() statement.

1 Like

@Rob , thank you so much, but one thing still blow my mind:
why myState have States type?
I mean States is Enumeration (enum).
my myState itself is not array/list/enumeration. It consist only one element of enum State.
It look like ordinary variable, like string OR it like “pointer” (maybe wrong term use) to list/array/enum element. So for why we need make it States type? Only to use dot notation like
myState = States.cell; ?

For my last question,

You haven’t provided your full script

yes, it’s my mistake. here it is:

public class TextController : MonoBehaviour {
	public Text text;
	private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
	private States myState;

	void Start () {
		text.text = "Long text example";
		Debug.Log(myState);
	}

	void Update () {
	}
}

Lok like myState have auto initialisation with 1st element from Enum, because I I don’t make any myState assignment before Debug.Log(myState)

1 Like

Your enum is a distinctive type which holds a set of constants.

The enum itself has an underlying type, by default this is an int but you can change it to others.

You don’t need to use the enum or use the dot notation, but trying to remember all of the different states by number for example would get incredibly difficult very quickly.

Regarding your script, ah I see… so your default value from your enum is returning as zero. Your enum values start at zero unless you specify otherwise, as such it is returning the first element, “cell”.

Hopefully this helps, additionally, see the link below, it covers the different underlying types, how to set different values etc.


See also;

  • MSDN C# Reference : enum
1 Like

Based on your example, let’s take a look at what each part of the Enumeration declarations do…

private enum States{lots of states}; 

This gives the compiler a handy list of possible states. Any States instance must be set to one of those possible states. (there is no State.undefined, unless you put that name in the list).

private States myState; 

This says I have a variable of type States… its value must be one of the values inside States. Depending on the compiler’s behavior, this will get auto assigned to either the first element in the enumeration, or any random value… ideally, when you declare, you’ll want to say:

private States myState=States.cell;

This ensure’s it’s set to a starting position.
That’s why

Debug.Log(myState); 

returns “cell”, because it was initialized. (If I remember correctly, Unity initializes all variables to 0, 0.0f, “”, NULL etc… cell is the 0th element of States). Good programming practice is to always initialize your variables, and not assume that your compiler will do it for you, because while Unity is friendly this way, not all programming languages are.

On to the States.cell/mirror/sheets, etc… The States in this part of the expression is the Qualifier… it says I’m choosing a State from States specifically. While there are many compilers who could accept the syntax:
private States myState=cell;
it is good coding practice to always qualify your state. Then when you’re reading the code a year from now, you know right away, that you’re choosing from one of the members of State.

Now… when you want to change the state later in the code (say… our prisoner is looking at the mirror, you would simply use

myState=State.mirror;
1 Like

@Rob thank you again, one more. Look like I did not correctly formulate the question.
Let me explaine it one more time: why myState have States type?
I mean why myState IS State type if it equal only to one element of enumeration?

Is not questioon why we need to use Enum (yes, your point is 100% true, it’s comfortable to use Enum), but why we need to use States as a Type for variable myState?

Why I can not write something like (just an idea, pseudo code to show a point):
var string myState = toString(State.cell);
so myString (for example) will be qeual to “cell” and after this we can use switch or if/else to choose programm flow.

I mean why String as type so important?

p.s. English is not my native language, I apologize for the tongue-tied language.

@DeusEx, that would actually be adding an incredible amount of complexity to your code.

By declaring

States myState=States.Cell;

your code comparison is much faster when it’s time to evaluate which state you are in, and it allows for things like the switch/case syntax.
Comparing two strings is time intensive… you can’t simply say if myString==“cell”, you have to compare each element in the string. An Enum is stored as a simple integer, it’s FAST and COMPACT compared to string comparisons…

switch(myState)
{ 
       case: States.cell: Dosomething();break;
       case: States.mirror: Dosomethingelse();break;
       ...
 }

compared to:

 if(String.Equals(myString,"cell",StringComparison.Ordinal))
 {
      DoSomething();
 } else
 if(String.Equals(myString,"mirror",StringComparison.Ordinal))
{
      DoSomethingElse();
 } ...

The first example will switch almost instantly, the 2nd example will require at least 4 comparisons per state, even with the most optimized code.

You can do it the second way, you’ll be working a lot harder in the long run. By using enum, the source code should be very readable to the programmer, function faster, and easier.

1 Like

This response helped me out greatly! I didn’t really realize that enum as a data type not only stores an enumerated list but also translates as it’s own data type. I was really thrown by the instructor declaring a variable on one line, and then using it as a type on the next! Thanks for the clarity.

Privacy & Terms