Jump to content

Visual Basic .NET/Enumerations

From Wikibooks, open books for an open world

Introduction

[edit | edit source]

An enumeration is a set of related constants defined by a value type. By default, an enumeration is set to integer and sets the first constant to 0 and the second to 1 and so forth. This is so when values have not been declared.

Setting Enumerations

[edit | edit source]

An enumeration that is set up with values 0, 1 and 2. (Values are set to integer as data type has not been declared):

  Enum Terms
      Net30Days
      Net60Days
      Net90Days
  End Enum

An enumeration that sets the constants to 30, 60 and 90.

  Enum TermValues As Short
      Net30Days = 30
      Net60Days = 60
      Net90Days = 90
  End Enum

Using Enumerations

[edit | edit source]

To use an enumeration, you have to call up the enumeration collection name first. Then, like looking into the methods of a class, you then declare the enumeration that you want to use.

  Dim t As Terms = Terms.Net30Days                    't = 0
  Dim i As Integer = CInt(Terms.Net30Days)            'i = 0
  Dim value As Integer = CInt(TermValues.Net60Days)   'value = 60
  Dim name As String = TermValues.Net30Days.ToString  'name = "30"