Sveučilište u Zagrebu  |  Filozofski fakultet  |  Služba za informatiku  |  Kutak za korisnike  |  Teme

Enum sample

Module Module1

    Enum days
        Monday = 1
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday = 0
    End Enum

    Sub Main()

        Dim sb As New Text.StringBuilder

        ' Enum.GetValues() returns an array of values in enum
        For Each dayValue In [Enum].GetValues(GetType(days))
            sb.AppendLine("Value: " & dayValue)
        Next

        ' Enum.GetNames() returns an array of names in enum
        For Each dayName In [Enum].GetNames(GetType(days))
            sb.AppendLine("Name: " & dayName)
        Next

        ' Enum.GetValues().Length returns a number of items in enum
        Dim numberOfDays As Integer = [Enum].GetValues(GetType(days)).Length

        ' Enum.GetName() returns one particular name for a given value
        For n = 1 To numberOfDays
            sb.AppendLine("LoopIndex " & n & ", intValue " & CType(n, days) & ", stringValue " & CType(n, days).ToString & ", day " & [Enum].GetName(GetType(days), n))
        Next

        ' Enum.GetValues() helps to catch them all if values are not sequential
        For Each dayValue In [Enum].GetValues(GetType(days))
            sb.AppendLine("IntValue " & CType(dayValue, days) & ", stringValue " & CType(dayValue, days).ToString & ", day " & [Enum].GetName(GetType(days), dayValue))
        Next

        Console.WriteLine(sb.ToString)

        Console.ReadKey()

    End Sub

End Module

 

komentare molim ovdje