Re: [Vala] Display values of an enum.



Bob Hazard wrote:
I had never heard of EnumClass or EnumValue until I saw your code but
after a quick play around with it the following seems to actually
work:


enum Something
{
      TEST1,
      TEST2,
      TEST3;
      
      public string toString ()
      {
              return ((EnumClass)typeof (Something).class_ref ()).get_value
(this).value_name;
      }
}

void main()
{
      Something e  = Something.TEST3;
      
      print (e.toString () + "\n");
}


bob tbird:~/Projects/obj$ valac enumtest.vala
bob tbird:~/Projects/obj$ ./enumtest
SOMETHING_TEST3

For this special case you do not need any extra code:

enum Something {
        TEST1,
        TEST2,
        TEST3
}

void main () {
        Something e = Something.TEST3;
        print ("%s\n", e.to_string ());

        // Or even shorter:
        print (@"$e\n");
}


Best regards,
Frederik



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]