Passing an enum as a paramater

  • 12 years ago

    Hello,

        I am trying to pass an enum as a parameter to and another class.  The class that is going to receive it does not know what the values of the enum are.  This class is going to be used throughout my program where I need to pass several different enum's through to it.  Does anyone know how to do this? 

    Thanks in advance.

     

  • 12 years ago

    Hello ICEMAN2303,

     

    => You can pass ‘my_enum’ or any other type of enumerations to the ‘test’ function, as a parameter :

    enum MyEnum

    {

    A = 0,

    B = 1,

    C = 3

    }

    MyEnum my_enum;

    private void test(Enum a)

    {

    Array consts = Enum.GetValues(a.GetType()); // enumeration constants

    string[] names = Enum.GetNames(a.GetType()); // names of enumeration constants

    a = (Enum)consts.GetValue(2); // set the enumeration with its first constant

    int first_value = (int)consts.GetValue(0); // set with the value of first enumeration constant

    if (a.Equals(consts.GetValue(consts.Length - 1)))

    MessageBox.Show("Enumeration has its last value");

    }

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson