Hello! Today we'll be covering enums
which is for short for enumerator. This tutorial is marked as intermediate
because it can be confusing and most likely wont be used by the beginning
programer.
What is an Enum?
MSDN: The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list.
English: An enum is a place to store and keep track of constants with integers values. These enums not only keep code more readable but can also make it easier to work with.
When to use Enums?
Enums should be used when you have multiple constants that pertain to each other enough to be placed in a single group. For example, let’s say you have these constants with integer values:
What is an Enum?
MSDN: The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list.
English: An enum is a place to store and keep track of constants with integers values. These enums not only keep code more readable but can also make it easier to work with.
When to use Enums?
Enums should be used when you have multiple constants that pertain to each other enough to be placed in a single group. For example, let’s say you have these constants with integer values:
Code:
const
int North = 0;
const
int East = 1;
const
int South = 2;
const
int West = 3;
The example North, East, South and
West, is a good example because they all relate to each other enough to fit in
one enum named "Direction", as we'll go over.
Declaring Enums.
The first thing to do is declare our enum followed by its name.
Declaring Enums.
The first thing to do is declare our enum followed by its name.
Code:
enum
Direction
{
};
I name our enum
"Direction" because this name pertains to its contents, and makes it
generally easier to remember.
Now we can add our constants inside both braces, making sure to add a comma before adding a new constant.
Now we can add our constants inside both braces, making sure to add a comma before adding a new constant.
Code:
enum
Direction
{
North = 0, //Notice
the keyword "const" and "int" are not needed.
East =
1, //Everything
inside and enum is automatically declared as constant.
South = 2,
West =
3
};
I mentioned the keyword
"int" is also not needed, this is because all enum constants are type
int by default, but they can be changed by assigning the integer type to the
end of the enum name.
Code:
enum
Direction : short
{
North = -32767,
East =
32767,
South = 2,
West =
3
};
As you can see I created
"short" constants, but you may also create byte, sbyte, ushort, int,
uint, long, and ulong constant types.
By default if the constants are un assigned they will automatically take the value of the the last constant in the enum plus one, starting from 0.
By default if the constants are un assigned they will automatically take the value of the the last constant in the enum plus one, starting from 0.
Code:
enum
Direction
{
North, // North = 0
automatically
East, // East = 1
automatically
South = 4, // This
will affects West's value.
West // West now =
5
};
Reference.
There is something else we can do with enums though, and that is you can create a special instance that can equal any of our constants values. The most common use for this is in a switch or, if else statement. Below is an example of how these statements work with our enum instance.
Code:
Direction
dir = Direction.North;
switch
(dir)
{
case Direction.North: //Our dir value is North so we'll see this.
MessageBox.Show("You picked
North.");
break;
}
if(dir
== Direction.North) //As we know, our dir value is
North so we'll also see this.
MessageBox.Show("You picked
North.");
If that was easy enough to understand then hopefully you'll
understand using user input to declare the value of "dir".
Code:
string
input = textBox1.Text; // Im using a textBox to get
input, you may use whatever you like.
Direction
dir = (Direction)Convert.ToInt32(input);
We receive the input from the user
and assign a new Direction. To understand how "dir" gets its value
we'll assume the constants are defined like so:
Code:
North = 0,
East =
1,
South = 2,
West =
3
(Its important to note that the user
types a number as input, not a word.)
Now, "dir"s value is based off the user's input, we'll assume the user inputs a number between 0 and 3. When we receive the input we assign "dir"s value to the input. To better understand let’s say the input is “0”, we have a constant equal to "0" in our enum, its name is "North". So "dir"s value would become North, likewise, if we get "1" as the input "dir"s value would become East.
Think of it like this; There are numbers in the enum that have names assigned to them, if we get the input "2" our "dir" value is 2. Now the name correlating with the value 2, is? South!
I hope that was understandable.
Now we can easily compare values through our enum instance with the true enum, like so.
Switch statement:
Now, "dir"s value is based off the user's input, we'll assume the user inputs a number between 0 and 3. When we receive the input we assign "dir"s value to the input. To better understand let’s say the input is “0”, we have a constant equal to "0" in our enum, its name is "North". So "dir"s value would become North, likewise, if we get "1" as the input "dir"s value would become East.
Think of it like this; There are numbers in the enum that have names assigned to them, if we get the input "2" our "dir" value is 2. Now the name correlating with the value 2, is? South!
I hope that was understandable.
Now we can easily compare values through our enum instance with the true enum, like so.
Switch statement:
Code:
string
input = textBox1.Text;
Direction
dir = (Direction)Convert.ToInt32(input);
switch
(dir)
{
case Direction.North: //If the input is "0" You'll see this.
MessageBox.Show("You picked
North.");
break;
case Direction.East: //If the input is
"1" You'll see this.
MessageBox.Show("You picked
East.");
break;
case Direction.South: //And so on...
MessageBox.Show("You picked
South.");
break;
case Direction.West:
MessageBox.Show("You picked
West.");
break;
}
Again, this time using an If Else
statement, which is slightly easier understand:
Code:
string
input = textBox1.Text;
Direction
dir = (Direction)Convert.ToInt32(input);
if(dir
== Direction.North) //If the input is "0" You'll see this.
MessageBox.Show("You picked
North.");
else
if(dir == Direction.East) //If the input is
"1" You'll see this.
MessageBox.Show("You picked
East.");
else
if(dir == Direction.South)
MessageBox.Show("You picked
South.");
else
if(dir == Direction.West)
MessageBox.Show("You picked
West.");
Before we leave off I should also mention that you can refence the constants direct value by casting it.
Code:
MessageBox.Show(Convert.ToString((int)Direction.North));
//Or a more readable version...
int
x = (int)Direction.North;
MessageBox.Show(x.ToString());
// Will show the value of North, which is
"0".
As you can
see, you dont always have to refer to your constant through an instance, it can
still be used like a normal constant.
No comments:
Post a Comment