Get enum string from integer

StewartBW 565 Reputation points
2024-05-16T01:15:44.92+00:00

Hello

I have this enum:

    Friend Enum Blah As Integer
        Zero = 0
        One = 1
        Two = 2
    End Enum

I will get an integer and need to return the related string.

ie will need to convert 1 to One.

Select Case InputInteger
 Case 0
  Return Zero
...
End Select

This will work but a short direct convertion?

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,612 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dewayne Basnett 1,361 Reputation points
    2024-05-16T13:30:27.7+00:00

    This should work also

        Dim foo As Blah = CType(InputInteger, Blah)
    
        Dim s As String = foo.ToString
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 27,406 Reputation points Microsoft Vendor
    2024-05-16T02:14:23.07+00:00

    Hi @StewartBW ,

    You can achieve a more concise conversion from an integer to the corresponding enum name by using the Enum.GetName method.

        Public Function ConvertIntegerToEnumName(value As Integer) As String
            Return [Enum].GetName(GetType(Blah), value)
        End Function
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments