Visual Basic Fundamentals: Development for Absolute Beginners

Enumerations and the switch Decision Statement - 19

Download this episode

Download Video

Download captions

Download Captions

Description

Classes can contain many different types of information, not just Properties and Methods. We begin demonstrating the use of Enumerations because often in the .NET Framework Class Library, properties can only be set to a pre-determined subset of possible values. To illustrate this point we create our own custom enumeration, and then utilize it in a simple application that demonstrates a third Decision statement, the switch. We demonstrate some Visual Studio IDE magic that will automatically implement code blocks for all possible enumeration values.

Download the source code for Enumerations and the switch...

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • Adam​Speight2008

      A found it a little misleading when you (Bob) refer to the ComplexIfStatement, kind of inferred or suggested that the Select Case decision statement can't do what the ComplexIfStatement does. (Especially with Option Strict Off)

      See Example Below.

      Module Module1
      
        Sub Main()
          SelectCase_Example()
          Console.WriteLine()
          SuperHero_Example()
          Console.ReadLine()
        End Sub
        Public Sub SelectCase_Example()
          Console.WriteLine("Select Case Example")
          Dim value = 42
          Select Case value
            Case Is < 1, Is > 100 : Console.WriteLine("Out of bounds")
            Case 23, 42, Is > 90 : Console.WriteLine("You Found one of the special numbers")
            Case Else
              Console.WriteLine("Not one of special numbers")
          End Select
        End Sub
        Public Sub SuperHero_Example()
          Console.WriteLine("SuperHero Example")
          GreetSuperHero(11)
          GreetSuperHero(SuperHeros.Batman)
        End Sub
      
        Public Sub GreetSuperHero(ByVal superhero As SuperHeros)
          Dim IsMemberOfSuperHeros = [Enum].IsDefined(GetType(SuperHeros), superhero)
          If IsMemberOfSuperHeros Then
            Console.WriteLine("Hello: {0}", superhero.ToString)
          Else
            Console.WriteLine("How Are You?")
          End If
        End Sub
        Enum SuperHeros
          Superman
          Batman
          Spiderman
        End Enum
      End Module
      

      When you use Enumerations it can be of any value that the underlying type can be. 

      See Example Code above

       

    • BobTabor

      @AdamSpeight2008:Awesome, awesome comment.  Thank you!  When we re-do this video I'll make sure to revise as you have advised.

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.