Enums

We will use an enum or enumerated type to keep up with each state that we want to switch through. Here, we have an enum structure with multiple states in it, such as SharingInit, ConnectingToServer, SpatialMapping, MapComplete, PlaceSkeeMachine, PlaceSkeeMachineComplete, StartGame, EndGame, and Reset.

The beauty of the enum structure is that while each element, as far as the computer is concerned is elements 1 - 9 ( or 0-8 since I have the = 0 after the first one), but to you or me as a programmer, it can be hard to keep up with which state number 6 is as opposed to PlaceSkeeMachineComplete.

An enum has many uses, and while it is not the most efficient means of producing code, it is very easy for a person to read and understand what is happening.

Let's say we want an enum for the states of my music system volume; it would look something like this:

enum MusicVolume
{
Loud,
Medium,
Soft,
Mute,
}

We would create a copy of it like this:

public MusicVolume myMusic;

We can use the preceding code to change the current state like this:

myMusic = MusicVolume.Soft;

Then, we can use it to do things with it, as follows:

if (myMusic==MusicVolume.Soft){
//change music volume to 2
}

You get the idea. As you will see in the next section, though, one great use of an enum is switch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.172.249