Gadzooks34
Gadzooks34

Reputation: 1828

Passing enumerated values to functions

Say I have a function that takes an integer as an argument. I'd like to be able to use an enumerated list as a way to keep the integer values organized.

For example, I'd ideally like to be able to define these (pseudocode):

public enum days
{
    monday,
    tuesday,
    etc...
}

public enum months
{
    january,
    february,
    etc...
}

int doSomething(enum x)
{
    return x + 1;
}

and then be able to call the function using either of the enumerated lists like this:

int a = doSomething(days.monday);
int y = doSomething(months.february);

This obviously won't work as-is because doSomething needs to be defined using just one of the enumerations (i.e. either days or months). I know of a couple of options. One is to simply cast to an int:

int a = doSomething((int)days.monday);
int y = doSomething((int)months.february);

The only problem with this is that this function gets called MANY places in my code, and it's clumsy to have to keep putting "(int)"s all over the place (one of the main motivations for grouping these int values together into enums in the first place is to make the code more readable).

Another option is to avoid enums altogether, and instead bundle the values into a container class, something like:

static class Days
{
    static int x = 0;

    static int monday = x++;
    static int tuesday = x++;
}

Again, this will work but just seems awfully cumbersome when I have a lot of these container classes to define.

The answer might very well be that there is no simpler way, and that I need to be a grown-up and just accept one of these options. But I thought I would get a sanity check on that before committing to it. Is there a third option?

Upvotes: 2

Views: 240

Answers (8)

Fabio
Fabio

Reputation: 32455

I spent a “little” time on this, because got a same problem(solution). So here is my solution which work fine on (.NET 4), Windows Forms:

VB:NET

           Private Function DoSomething(Of TEnum As {IComparable, IConvertible, IFormattable})(ByVal valueEnum As TEnum) As Int32

              Dim i As Int32 = CInt(Convert.ChangeType(valueEnum, valueEnum.GetTypeCode()))
              //Do something with int
              i += 1
              Return i

           End Function

    C#

        private int DoSomething<TEnum>(TEnum valueEnum) where TEnum: IComparable, IConvertible, IFormattable 
                    {
                        int i = 0;
                        i = (int)Convert.ChangeType(valueEnum, valueEnum.GetTypeCode());
                        i++;
                        return i;
                    }

Upvotes: 0

Fabio
Fabio

Reputation: 32455

DId you try to overload a function by parameter:

int DoSomething(Enum1 value)
int DoSomething(Enum2 value)

Upvotes: 0

abatishchev
abatishchev

Reputation: 100288

What is your issue?

public enum Days : short
{
    Monday = 1,
    Tuesday = 2,
    ...
}

DoSomething(Days.Monday);

void DoSomething(Days day)
{
    Days nextDay = day + 1;
}

Also note already built-in enum System.DayOfWeek.


I got OP's point but afaik this is not supported by C# yet:

void DoSomething<T>(T e) where T : enum
{
    T next  = e + 1;
}

Upvotes: 2

Praveen
Praveen

Reputation: 100

public enum days : int 
{ monday, tuesday,

}

public enum months :int
{ january, february, march,

}

public  int doSomething(int z)
{
  return z + 1;
}

// your calling method int c = ee.doSomething((int)testenums.months.march); int c = ee.doSomething((int)testenums.day.February);

working code as you always pass enum and which is of type int you just need to parse and send this code perfectly works.. let me know

Upvotes: 0

Eyal H
Eyal H

Reputation: 989

Or if you don't want to change all existing Enums:

    public static int DoSomething(Enum x) 
    {
        int xInt = (int)Convert.ChangeType(x, x.GetTypeCode());
        DoSomething(xInt);
    }

as stated here: enums-returning-int-value

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

yes you can do so

public enum days : int
{
    monday,
    tuesday,
    ...
}

automatically monday becomes 0 and tuesday becomes 1 and so on

public enum months : int
{
    january,
    february,
    ...
}

same for months

int doSomething(Enum x)
{
    return (int)x + 1;
}

and call it as

int a = doSomething(days.monday);

or call it as

int a = doSomething(months.january);

now days.monday equals 0 and after method a becomes 1.

Upvotes: 0

Sconibulus
Sconibulus

Reputation: 732

You could overload your method, if you really just want the int value, perhaps something like this?

int dosomething(enum x)
{return dosomething((int)x)}
int dosomething(int x)
{return x+1}

Upvotes: 0

oleksii
oleksii

Reputation: 35905

Any problem with System.DateTime?

This would be the most pragmatic type to use.

Upvotes: 0

Related Questions