0% found this document useful (0 votes)
69 views17 pages

Enums

Enums allow assigning names to constant number values for reliability. They behave like value types where the value is copied. An example is the DayOfWeek enum with Sunday-Saturday values. Enums are more reliable than strings for comparisons since they are strongly typed and avoid typos. To create an enum, define the possible values separated by commas within an enum class. This gives names to integer values used as constants. Enums were used to define an EmployeePosition for more maintainable code over string validation.

Uploaded by

Dez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views17 pages

Enums

Enums allow assigning names to constant number values for reliability. They behave like value types where the value is copied. An example is the DayOfWeek enum with Sunday-Saturday values. Enums are more reliable than strings for comparisons since they are strongly typed and avoid typos. To create an enum, define the possible values separated by commas within an enum class. This gives names to integer values used as constants. Enums were used to define an EmployeePosition for more maintainable code over string validation.

Uploaded by

Dez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Enums

Over the past few days we’ve talked about different types of data namely classes and all of the different
types of classes and interfaces and there’s another type of data called and Enum. And Enums allow us to
assign names to constant number values it kind of sounds like a variable but it is not.

In fact whenever we see it in our code it’s going to look a lot like a static class, but then again it’s not a
static class. Enums are value types so they behave just like every other value type, whenever we assign a
value from an Enum to a variable a copy of that value is being made so in memory the actual value is
being stored.

Before we write an Enum let’s look at one that already exists within the .NET Framework. The one I’m
going to pick is called Day of the week, and as you may think it is supposed to represent a day of the
week.
So whenever we look at Intellisense we see Sunday through Saturday. As I said it looks a lot like a static
class but it’s not one.

Let’s pick Friday.


And just like any other type of value we can store these values within a variable. Just like that, so the
variable day contains the value of Friday.

Now you might think why would we do this, why can’t we just use strings, well let’s look at that.
And let’s say that we have some code that’s supposed to do something on a Friday, so we want to do an
if statement, if day equals Friday, then do something.

Well, this do something is never going to execute because I’m human, I made a mistake I made a typo
while typing this string Fridray. So this is one problem that Enums solve, it takes the human-ness out of
our code because instead of performing a comparison with strings we perform a comparison with
Enums.
So let’s look at what this would look like, first let’s comment out our string code.

And then if day equals DayOfWeek Friday then do something. It is a whole lot easier and the compiler is
going to check for us so if we make any mistake whatsoever the compiler is going to say, you made a
mistake, fix it because Enums are strongly typed.
Before we proceed delete the highlighted code.

So Enums are more reliable and that reliability comes from the fact that they are constant values, so I
can think of a place in our Employee class where Enums would be very helpful.

And that is the position because as it is right now,


these are strings and while we have a set number of values for position like owner manager, sales clerk,
we can always add to those positions and any change will require us to be very careful with this string
values that we are using.

Well, that’s just a headache in my opinion if we use Enums then our maintainability just goes through
the roofs, so let’s create an Enum. And we can do so by going to add, we can choose Class and we will
eventually choose class but if we go to New Item…
…we will not see Enum listed anywhere here in the choosing list. And that’s unfortunate.

So we will need to use either class or interface. Class is just easier to use.
And then we will need to name our Enum, I’m going to call this EmployeePosition because we want to
be as specific as possible.

And then all we have to do is change Class…


…to enum and then we might want to specify public as well but that is basically how we create an Enum,
instead of saying class or interface we say Enum.

Now we just need to provide the possible values contained within this Enum and we do that very easily
we just start typing the words. So owner, manager, sales clerk and we cannot put spaces in-between
the words they have to be together. So we take the possible values and we separate them with commas
and that’s it.
Now behind the scenes the compiler is giving each of these values a numeric value, in fact they are
integers. The first one is 0, manager is 1, sales clerk is 2 and stocker is 3.

But by specifying this syntax that I’ve done here we can give them our own values if we’d like but for the
most part whatever the compiler gives it it’s going to be fine because the value typically doesn’t matter
it is just we are using these as constants. So we can modify the values but we don’t need to in this case.
So remove the numerical inputs, and let it look as shown below.
And also whatever we define as the value for the first item, let’s say 12, manager is going to be 13, sales
clerk 14, stocker 15. So it will take whatever the last value was that we assigned and then enumerate
from then on until we provide another value.

So we could do 50 here, so owner would be 12, manager would be 13, sales clerk would be 50, stocker
would be 51.
But again there are sometimes when we would want to do that but for the most part we care about the
constant-ness of these values, so let us not assign numerical value.

So let’s go to our Employee class, we need to change two things, we need to change the position
parameter…
…and the Position property.

So that’s very to do, all we have to do is specify that this is an EmployeePosition.


And we need to do the same thing for our Property, so EmployeePosition.

And then delete the validation code because we don’t need to do any validation at all because position
has a value now, it’s either owner, manager, sales clerk or stocker.
So we don’t have to validate the Position anymore.

Let’s go to Program.cs and let’s create an Employee object. We will give him a first name of John, Doe.
We will say that he is the manager and that he has a salary of $200,000 and that’s it.

Instead of using a string now, which we could cause some issues with, we are using an Enum, making
our code far more maintainable than it was before while also making it easier to read at least in my
opinion and far more reliable because we will always have a value for position. And we no longer have
to write a validation code for the position.

So that’s Enums, there’s one other type of data that we are going to talk about called a Strut, and in-fact
we will look at a Strut called Daytime which can be used in conjunction with an Enum day of week.

You might also like