Enumertors in VB.NET

This article defines the Enumerator with a example in VB.NET.
  • 1606

In this article You will learn How to use Enumerators in VB.NET.

Enumerators:

  1. Use enum keyword.

  2. Starts with 0 and incremented by 1.

  3. Enumeration is a related set of constants. They are used when working with many constants of the same type. It's declared with the Enum keyword. 

For example:


enum Days as byte

Sun=1

Mon

Tue

 

dim d as days=days.Tue;


- Default allocation is for int and You can define the datatype.

 

enum Numbers as byte

ZERO

ONE

TWO

THREE

 

Note:

Use sizeof() operator to get size of items

 

Code define the Enumerator:

 

Module Module1

    Enum Days As Byte

        Sun = 1

        Mon

        Tue

        Wed

    End Enum

    Class EnumTest

        Public Shared Sub Main()

            Dim d As Days = Days.Tue

            Console.WriteLine(d)

            Console.WriteLine(CInt(d))

        End Sub

    End Class

 End Module

 

Output:


enum.gif

 

This article shows that the value start with the zero and is increment by 1. sun day value is define 1 and it automatically increment the value by 1 till the tue. so the tue value is 3 .

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.