String to Enum?

StewartBW 545 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,610 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dewayne Basnett 1,361 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