digitalmars.D.learn - How to parse enum from a string ?
- Vinod K Chandran (13/13) May 27 2020 Hi all,
- WebFreak001 (7/20) May 27 2020 you can convert an enum to a string using
- Dennis (8/11) May 27 2020 Use `to` from `std.conv`.
- Vinod K Chandran (3/15) May 27 2020 Hi,
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 ?
 May 27 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
 May 27 2020
On Wednesday, 27 May 2020 at 17:33:33 UTC, Vinod K Chandran wrote: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 ?Use `to` from `std.conv`. ``` import std.conv: to; void main() { assert("Received".to!TestEnum == TestEnum.Received); } ```
 May 27 2020
On Wednesday, 27 May 2020 at 17:36:35 UTC, Dennis wrote:On Wednesday, 27 May 2020 at 17:33:33 UTC, Vinod K Chandran wrote:Hi, Thanks a lot. It worked. :)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 ?Use `to` from `std.conv`. ``` import std.conv: to; void main() { assert("Received".to!TestEnum == TestEnum.Received); } ```
 May 27 2020








 
  
  
 
 WebFreak001 <d.forum webfreak.org>
 WebFreak001 <d.forum webfreak.org> 