Home All Groups Group Topic Archive Search About

Reflection, Enums and their values?

Author
19 Oct 2006 3:29 PM
HockeyFan
I have an set of enums like the following, that are in a class that I'm
using from elsewhere;

            Public Enum Q001resp As Integer
                Yes = 1
                No = 5
                NotAnswered = 0
            End Enum

Using Reflection, I am able to look at the properties of that class,
and I'd like to be able to not only get the names of these enums, but
also their possible values.
For instance, I think I can get the Q001resp name (if I recall, I've
done this before, but can't say for sure).
I'd like to be able to get the string of "Yes" and the value of 1, the
string of "no" and the value of 5, and the "NotAnswered" and value of
0.
Is this possible with reflection?

Author
19 Oct 2006 3:37 PM
Carl Daniel [VC++ MVP]
HockeyFan wrote:
Show quote
> I have an set of enums like the following, that are in a class that
> I'm using from elsewhere;
>
>            Public Enum Q001resp As Integer
>                Yes = 1
>                No = 5
>                NotAnswered = 0
>            End Enum
>
> Using Reflection, I am able to look at the properties of that class,
> and I'd like to be able to not only get the names of these enums, but
> also their possible values.
> For instance, I think I can get the Q001resp name (if I recall, I've
> done this before, but can't say for sure).
> I'd like to be able to get the string of "Yes" and the value of 1, the
> string of "no" and the value of 5, and the "NotAnswered" and value of
> 0.
> Is this possible with reflection?

Yes.

Use Type.GetMembers() on the Enum type to get the members.  The members will
include all of the enumerators and a couple others with funny names (sorry,
I don't have the reference materials handy to look it up - just do some
experimenting).

You can get the value of a given enumerator using
Enum.Parse("Yes",typeof(Q001resp)).  I think there's a way to get that
directly from reflection as well (because presumably that's how Enum.Parse
does it under the covers).

-cd
Author
19 Oct 2006 5:19 PM
Dave Sexton
Hi,

It's possible without reflection too.  I coded this example in C#, but the concepts can easily be applied to VB as well:

public enum Q001resp
{
    NotAnswered,
    Yes,
    No = 5
}

class Program
{
    static void Main(string[] args)
    {
        // Iterate the values produced by the Enum.GetValues method,
        // which returns an array of the underlying enum type
        foreach (int value in (int[]) Enum.GetValues(typeof(Q001resp)))
        {
            Console.WriteLine("{0} = {1}",
                (Q001resp) value,
                value);
        }

        // And you can get the name and value of a single constant too
        // without reflection:
        Console.WriteLine("{0} = {1}",
            Q001resp.NotAnswered,
            (int) Q001resp.NotAnswered);
    }
}

Output:

NotAnswered = 0
Yes = 1
No = 5
NotAnswered = 0

--
Dave Sexton

Show quote
"HockeyFan" <les.stock***@gmail.com> wrote in message news:1161271792.039784.43500@b28g2000cwb.googlegroups.com...
>I have an set of enums like the following, that are in a class that I'm
> using from elsewhere;
>
>            Public Enum Q001resp As Integer
>                Yes = 1
>                No = 5
>                NotAnswered = 0
>            End Enum
>
> Using Reflection, I am able to look at the properties of that class,
> and I'd like to be able to not only get the names of these enums, but
> also their possible values.
> For instance, I think I can get the Q001resp name (if I recall, I've
> done this before, but can't say for sure).
> I'd like to be able to get the string of "Yes" and the value of 1, the
> string of "no" and the value of 5, and the "NotAnswered" and value of
> 0.
> Is this possible with reflection?
>
Author
19 Oct 2006 5:55 PM
Dave Sexton
Hi,

I just realized that Q001resp sounds auto-generated, in which case you might be asking about reflection because you don't have a
reference to Q001 when you're building your library; however, you still don't need to use reflection.  The Enum class provides all
that you need (Just realize that the Enum methods themselves probably use reflection, as Carl pointed out).

The following modifications to my original example still produce the exact same output:

public enum Q001resp
{
    NotAnswered,
    Yes,
    No = 5
}

class Program
{
    // We'll use the Type of the Enum (because you don't know the concrete
    // implementation at compile-time)
    private static Type eType = typeof(Q001resp);

    // And here's a reference to one of the Enum's values
    private static object eVal = Q001resp.NotAnswered;

    static void Main(string[] args)
    {
        foreach (object value in Enum.GetValues(eType))
        {
            // One way to extract the integral value is through formatting {1:d}
            Console.WriteLine("{0} = {1:d}", value, value);
        }

        // And you can get the name and value of a single constant too
        // without reflection:
        Console.WriteLine("{0} = {1}",
            eVal,
            // another way to extract the integral value is a simple cast
            Convert.ChangeType(eVal, Enum.GetUnderlyingType(eType)));
    }
}

HTH

--
Dave Sexton

Show quote
"Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message news:%23Qs4OL68GHA.3736@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> It's possible without reflection too.  I coded this example in C#, but the concepts can easily be applied to VB as well:
>
> public enum Q001resp
> {
>    NotAnswered,
>    Yes,
>    No = 5
> }
>
> class Program
> {
>    static void Main(string[] args)
>    {
>        // Iterate the values produced by the Enum.GetValues method,
>        // which returns an array of the underlying enum type
>        foreach (int value in (int[]) Enum.GetValues(typeof(Q001resp)))
>        {
>            Console.WriteLine("{0} = {1}",
>                (Q001resp) value,
>                value);
>        }
>
>        // And you can get the name and value of a single constant too
>        // without reflection:
>        Console.WriteLine("{0} = {1}",
>            Q001resp.NotAnswered,
>            (int) Q001resp.NotAnswered);
>    }
> }
>
> Output:
>
> NotAnswered = 0
> Yes = 1
> No = 5
> NotAnswered = 0
>
> --
> Dave Sexton
>
> "HockeyFan" <les.stock***@gmail.com> wrote in message news:1161271792.039784.43500@b28g2000cwb.googlegroups.com...
>>I have an set of enums like the following, that are in a class that I'm
>> using from elsewhere;
>>
>>            Public Enum Q001resp As Integer
>>                Yes = 1
>>                No = 5
>>                NotAnswered = 0
>>            End Enum
>>
>> Using Reflection, I am able to look at the properties of that class,
>> and I'd like to be able to not only get the names of these enums, but
>> also their possible values.
>> For instance, I think I can get the Q001resp name (if I recall, I've
>> done this before, but can't say for sure).
>> I'd like to be able to get the string of "Yes" and the value of 1, the
>> string of "no" and the value of 5, and the "NotAnswered" and value of
>> 0.
>> Is this possible with reflection?
>>
>
>

AddThis Social Bookmark Button