Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to write a method "AddValues" that will accept enumerations of integers
Posted

Like this?

C#
enum MyEnum
{
    Value1,
    Value2,
    Value3
}

void AddValues(int enumValue)
{
}

void Test()
{
    AddValues((int)MyEnum.Value1);
}
 
Share this answer
 
Comments
sunil6600 10-Nov-10 12:53pm    
expecting AddValues to take enum as parameter, like void AddValues(system.enum enum1)
Or like this: (look at the enumerator call in void Test)

C#
enum MyEnum
{
    Value1,
    Value2,
    Value3
}
void AddValues(MyEnum enumValue)
{
    System.Diagnostics.Debug.WriteLine(enumValue.ToString());
}
void Test()
{
    AddValues(MyEnum.Value1);
    AddValues(MyEnum.Value2);
    AddValues(MyEnum.Value3);
}
 
Share this answer
 
OK, take two. You could do this but it wouldn't make much sense. Why do you need to pass an integer in the first place? If it comes from somewhere else, you should check and decode it to a meaningful value first.

C#
enum MyEnum
{
    Value1,
    Value2,
    Value3
}

void AddValues(MyEnum enumValue)
{
    System.Diagnostics.Debug.WriteLine(enumValue.ToString());
}

void Test()
{
    AddValues((MyEnum)1);
    AddValues((MyEnum)2);
    AddValues((MyEnum)4);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900