String to Enum?

StewartBW 1,145 Reputation points
2024-05-15T16:39:50.26+00:00

Hello,

How to assign a string to an enum in vb.net?

DirectCast([Enum].Parse(GetType(Blah), "Second"), Integer)

Option Strict On disallows implicit conversions from 'Integer' to blah.
Public Enum Blah
    First
    Second
    Thirds
End Enum
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,749 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dewayne Basnett 1,366 Reputation points
    2024-05-15T16:51:06.7933333+00:00

    It should be like this I think,

        Dim blahVal As Blah = Blah.First
    
        Dim parse As String = "second"
    
        blahVal = DirectCast([Enum].Parse(GetType(Blah), parse, True), Blah)
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.