use any enumerator as function parameter

A

ajmastrean

I have a bunch of enumerators. I need to pass them into a function that
is designed to write all values of any enumerator to the console. For
instance...

public enum Days : int
{
Sunday = 1,
Monday,
Tuesday
}

public enum Colors : int
{
red = 1,
blue,
green
}

static void main ()
{
Disp(Colors);
}

public static void Disp(enum e)
{
string[] sa = System.Enum.GetNames(typeof(e));

foreach(string i in sa)
{
Console.Writeline(i);
}
}

I cannot do this, the compiler fails on the "enum e" parameter in a
variety of ways... is there any way to pass an enumerator? I have
tested the code with a specific Enumerator in place of e and it works
fine... but I need it to be reusable (yeah, a 3 line function!).

Any info would be great. Thanks.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| public static void Disp(enum e)
| {
| string[] sa = System.Enum.GetNames(typeof(e));
|
| foreach(string i in sa)
| {
| Console.Writeline(i);
| }
| }
|
| I cannot do this, the compiler fails on the "enum e" parameter in a
| variety of ways... is there any way to pass an enumerator? I have
| tested the code with a specific Enumerator in place of e and it works
| fine... but I need it to be reusable (yeah, a 3 line function!).

Try this :

public static void Disp(Enum e)
{
string[] sa = System.Enum.GetNames(typeof(e));

foreach(string i in sa)
{
Console.Writeline(i);
}
}

BTW, an enumerator is something that enumerates or iterates over a
collection of items, what you are using here is an *enumeration* :)

Joanna
 
M

Morten Wennevik

Hi Ajam,

You need to rewrite a few lines. First enum is a C# keyword, while Enum
(capital e) is the framework name. To get your code to work you need to
pass a value from your Enum, not the Enum itself. I also changed your
typeof(e) to e.GetType() since e is a member of Enum, not Enum itself.

static void Main()
{
Disp(Colors.red);
}

public enum Colors : int
{
red = 1,
blue,
green
}

static void Disp(Enum e)
{
string[] sa = System.Enum.GetNames(e.GetType());

foreach (string i in sa)
{
Console.WriteLine(i);
}
}

Since it basically is the type of the enum you are looking for, you could
also write it like this, passing the type as parameter instead of enum.

static void Main()
{
Disp(typeof(Colors));
}

public enum Colors : int
{
red = 1,
blue,
green
}

static void Disp(Type t)
{
string[] sa = System.Enum.GetNames(t);

foreach (string i in sa)
{
Console.WriteLine(i);
}
}
 
R

Roman Wagner

try this

using System;

namespace EnumPrint
{
enum e1
{
a,b
}

enum e2
{
c,d
}
class MainClass
{
private static void PrintEnum(Type enumType)
{
if(!enumType.IsEnum)
{
Console.WriteLine(String.Format("{0}: no enum", enumType));
return;
}

Console.WriteLine(enumType);
foreach(int i in Enum.GetValues(enumType))
{
Console.WriteLine(String.Format("{0} = {1}",
Enum.GetName(enumType,i), i));
Console.WriteLine();
}
}

public static void Main(string[] args)
{
PrintEnum(typeof(e1));
PrintEnum(typeof(e2));
PrintEnum(typeof(DateTime));
Console.Read();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top