How to parse enum from a string ?

Andersen andersnesr541 at gmail.com
Wed Dec 28 00:41:19 UTC 2022


On Wednesday, 27 May 2020 at 17:33:33 UTC, Vinod K Chandran wrote:
> Hi all,
> Assume that i have an enum like this.
> enum TestEnum  {
>     Received = 1,
>     Started ,
>     Finished ,
>     Sent
> }
>
> I am saving this enum values as string in database. So, when i 
> retrieve them from the database, how can i parse the string 
> into TestEnum ? In vb. net, i can use
> [Enum].Parse(GetType( TestEnum), "Started")
> How to do this in D ?

Parsing enum from a string can be done using the Enum.Parse() 
method. First, the enum type is passed into the method as a 
parameter. Then, the string to be parsed is passed in as the 
second parameter. If the string cannot be parsed into the enum 
type, then a exception will be thrown. To avoid this, the 
TryParse() method can be used instead, which will return a 
boolean value.

https://andersenlab.com/blueprint/choosing-the-best-test-automation-frameworks


More information about the Digitalmars-d-learn mailing list