Re: [Vala] Working with enums in Vala



Martin Olsson wrote:
Hi,

What do people do in Vala today in order to:
A) print an enum value to the screen? (in C# Enum.GetName() can be used)
B) Convert an int or string to some enum? (for instance if you have the
int 3 but the enum only contains 1 and 2 then I want some runtime error.
In C# this would be the Enum.Parse() method).

Ideally I don't want to implement these functions myself because then I
must update the functions whenever I change the enum (that will be
unnecessarily error prone).


Today I found out over IRC that there is a way to do this in Vala. In case anyone
finds this e-mail thread on Google I thought I'd include the answer below.
It's not pretty but it works (no need to update this code every time a new
value is added to the enum):

string enum_value_as_string = ((EnumClass) typeof (MyEnum).class_ref ()).get_value (enum_value).value_name;

If anyone knows a a less complicated and more memorable way to do the same
thing please reply to this thread. In the long term it would be nice to have
something that returns the "Vala name" of the enum and not the C name.



                Martin



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