+ Reply to Thread
Results 1 to 2 of 2

Thread: VB.NET from beginner to advanced programmer Part 19 - Enumerations

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 19 - Enumerations

    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
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. 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:

    Code:
    Private Enum MyEnum
        Member1
        Member2
        Member3
    End Enum
    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).

    We can also give the members specific values:


    Code:
    Private Enum MyEnum
        Member1 = 10
        Member2 = 15
        Member3
    End Enum
    Since we haven't given Member3 any value it will get the value of the member above + 1(16).




    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:

    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
    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.




    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.

    VB.NET from beginner to advanced programmer Part 19 - Enumerations-enums.png




    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 19 - Enumerations

    Very cool and useful! +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VB.NET from beginner to advanced programmer Part 3 - Variables
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-02-2009, 05:18 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. VB.NET from beginner to advanced programmer Part 10 - Arrays
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-02-2009, 05:45 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts