How to parse enum from a string ?

WebFreak001 d.forum at webfreak.org
Wed May 27 17:36:34 UTC 2020


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 ?

you can convert an enum to a string using
myEnum.to!string

and you can similarly convert strings back to the enum using
someString.to!TestEnum

(import std.conv for both of them)

Example: https://run.dlang.io/is/UeLjJS


More information about the Digitalmars-d-learn mailing list