ToString()

列舉上的 ToString 方法返回列舉的字串名稱。例如:

Module Module1
    Enum Size
        Small
        Medium
        Large
    End Enum

    Sub Main()
        Dim shirtSize As Size = Size.Medium
        Dim output As String = shirtSize.ToString()
        Console.WriteLine(output)  ' Writes "Medium"
    End Sub
End Module

但是,如果需要列舉的實際整數值的字串表示,則可以將列舉轉換為 Integer 然後呼叫 ToString

Dim shirtSize As Size = Size.Medium
Dim output As String = CInt(shirtSize).ToString()
Console.WriteLine(output)  ' Writes "1"