How to compare an enum value and a string that should match the enum name in VB.NET

1 Answer

0 votes
Imports System

Class Program
	Enum EN
        A = 0
        B = 1
        C = 2
    End Enum

    Public Shared Sub Main()
        ' Checks if the name of EN.C is "C".
		Console.WriteLine([Enum].GetName(GetType(EN), EN.C) = "C")
        
		' Converts EN.C to string and compares it to "C".
		Console.WriteLine(EN.C.ToString() = "C")
		
		' Parses the string "C" into the enum and checks if it equals EN.C.
        Console.WriteLine(CType([Enum].Parse(GetType(EN), "C"), EN) = EN.C)
    End Sub
End Class
 
 
 
' run:
' 
' True
' True
' True
'

 



answered Aug 1, 2025 by avibootz
edited Aug 1, 2025 by avibootz

Related questions

...