Jump to content

Could Someone Explane Enums?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
AtoZ

AtoZ

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Could someone give me some very basic examples and some explanations of what enums do?

I'm following a tutorial but it's not really explained them before use, and the MSDN page is a little over complexing it.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is a way to store multiple items in sequential order, each with its own numeric index. An example:
enum Rating {Poor, Average, Okay, Good, Excellent}

Rating IncrementRating(Rating r)
{
    if(r == Rating.Excellent)
        return r;
    else
        return r+1;
}
Rating DecrementRating(Rating r)
{
    if(r == Rating.Poor)
        return r;
    else
        return r-1;
}
As you can see there is a good use in this, for example defining error codes or messages, as you can compare them as integers by name. Some more examples can be found here:
Enums and Structs in C# - CodeProject
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users