Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
In this part it's time for something called Enums which is short for Enumerations. Enums is used when you have some unchanging values which is related to each other in some way or another. So then you "group" them together and give them a name.
You create an Enum like this:
So here we created an Enum called MyEnum with the three members: Member1 (which got the value 0), Member2 (value 1) and Member3 (Value 2).Code:Private Enum MyEnum Member1 Member2 Member3 End Enum
We can also give the members specific values:
Since we haven't given Member3 any value it will get the value of the member above + 1(16).Code:Private Enum MyEnum Member1 = 10 Member2 = 15 Member3 End Enum
I understands if you didn't understood how, when and why to use Enumerations. I show you an example to explain it better:
Without Enums:
With an ID of an animal/object we uses a function to get the number of legs it have. We have to remember all IDs to get it right.
Code:Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(Legs(1)) End Sub Private Function Legs(ByVal animal As Integer) Select Case animal Case 0 'Snake Return 0 Case 1 'Dog Return 4 Case 2 'Human Return 2 Case 3 'Three legged table Return 3 Case 4 'Cat Return 4 End Select End Function
With Enums
Now instead we'll create an Enum called Animals, we've changed the parameter to be of the type Animals, and changed the Select Case statements' values too:
Code:Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(Legs(Animals.Dog)) End Sub Private Function Legs(ByVal animal As Animals) Select Case animal Case Animals.Snake Return 0 Case Animals.Dog Return 4 Case Animals.Human Return 2 Case Animals.Table Return 3 Case Animals.Cat Return 4 End Select End Function Private Enum Animals Snake Dog Human Table Cat End Enum
That was the version which looked liked the one without Enums but were a lot easier to read and use. But we can completely remove the Select Case statement and the function, like so:
In the code above I gave all members in the Enum the value we wanted. Then I just show "Animals.Dog" in a messagebox. The output was 4.Code:Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(Animals.Dog) End Sub Private Enum Animals Snake = 0 Dog = 4 Human = 2 Table = 3 Cat = 4 End Enum
A very good thing with Enums is that you'll have a list of all the values as you can see in the example below. That example is showing all members of the Enum DialogResult which is used to see which button that was pressed on a messagebox. So now you also got an example on an inbuilt Enum.
That was pretty much it. Hope you understood how, when and why to use Enumerations. In the next part I'll teach you about some more advanced commenting. Till then.
Last edited by Vswe; 03-21-2010 at 03:38 PM.
Very cool and useful! +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks