473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

use any enumerator as function parameter

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.

Nov 1 '06 #1
3 3169
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();
}
}
}

Nov 1 '06 #2
<aj********@gmail.coma écrit dans le message de news:
11*********************@b28g2000cwb.googlegroups.c om...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 1 '06 #3
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);
}
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pablo | last post by:
Dear Newsgroupers, The 'main' page contains a call to a function in an included file. This function puts a html-form on the screen. Before the form gets posted (to the 'main' page) some prior...
1
by: Alex Vinokur | last post by:
========================= Windows 2000 Professional Digital Mars C/C++ 8.36 STLport 4.5.3 ========================= I have got a problem with compilation of the following piece of code using...
10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
10
by: Juke All | last post by:
When I compile the code (below), I get this error: cannot convert parameter 1 from 'int' to 'union dna' Without saying: FOO x; x.val = 100; ....is it possible to use a union as a function...
5
by: Rolf Wester | last post by:
Hi, I want to pass a C-function as a function parameter but I don't know how to that correctly. In the example below how would I have to declare the function argument in the my_sort function...
2
by: Glenn Lerner | last post by:
If I pass a reference type (such as DataSet) to a function, I'm assuming only a reference is passed (not a copy). So there is no need to declare function parameter as ref for those types? Example:...
5
by: Joe | last post by:
Hi, I like to know what do you specify in the function parameter (in the function implementation) if you want the string that you pass in with the function call to be changed while its in the...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
3
by: giloosh | last post by:
can i pass a hash as a function parameter. ive seen it been used before but i can't figure out how to do it. i would like to call a function like this for example ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.