On Fri, 2011-09-30 at 11:45 +0200, tomw wrote:
> Hi,
>
> I'm trying to get enum values based upon a match to their string
> representation. To do that, I would need to iterate over the enums. I
> was wondering whether there is a simple way of doing it without creating
> an array first?
>
> Thanks,
>
Hi,
you can try with this:
public enum TestEnum
{
ZERO,
ONE,
TWO,
THREE,
FOUR
}
public static void main ()
{
EnumClass klass = (EnumClass) typeof(TestEnum).class_ref ();
for (int i = 0; i < klass.n_values; i++)
{
var enum_value = klass.get_value (i);
if (enum_value == null) {
print ("No value for: %d\n", i);
} else {
print("Enum (%d): %s [%s] = %d\n", i,
enum_value.value_name,
enum_value.value_nick, enum_value.value);
}
}
string[] words = new string[] {
"zero", "one", "two", "<no enum for me>"
};
foreach (string word in words) {
var enum_value = klass.get_value_by_nick (word);
if (enum_value == null) {
print ("No value for: %s\n", word);
} else {
print("Enum (%s): %s [%s] = %d\n", word,
enum_value.value_name,
enum_value.value_nick, enum_value.value);
}
}
}
Best regards,
Andrea
_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list