is it possible to receive enum as argument to method

G

GS

I does not seem possible even with Enum Class or at least I don't know how.


I can pass myenum.something but just could not find a way to pass myenum
where myenum is
enum myenum{something,whatever};
I am working with .net 3.5 sp1

why would I want enum paramater? I am working on a customizable component
and need the called to pass the complete enum to select available component
action
and ignore any additional items

yes there will have to be check about the parameter
 
H

Harlan Messinger

GS said:
I does not seem possible even with Enum Class or at least I don't know how.

public enum MyEnum { Something, AnotherThing, YetAnotherThing }

....

public string MyMethod(MyEnum x)
{
switch x
{
case MyEnum.Something: return "Hello";
case MyEnum.AnotherThing: return "Good bye";
case MyEnum.YetAnotherThing: return "Aloha";
}
}
 
K

kndg

I does not seem possible even with Enum Class or at least I don't know how.


I can pass myenum.something but just could not find a way to pass myenum
where myenum is
enum myenum{something,whatever};
I am working with .net 3.5 sp1

why would I want enum paramater? I am working on a customizable component
and need the called to pass the complete enum to select available component
action
and ignore any additional items

yes there will have to be check about the parameter

Something like below?

public enum MyEnum { Something, Whatever };

public class MyClass
{
public static void Main(string[] args)
{
PrintEnumMembers(new MyEnum());
}

public static void PrintEnumMembers(Enum myEnum)
{
foreach (var item in Enum.GetNames(myEnum.GetType()))
{
Console.WriteLine(item);
}
}
}

But, I don't think that would be the right approach to your problem.

Regards.
 
G

GS

thx

so I would be best to declare the enum at the global level as public scope.
this way, the enum can be overridden on invocation of the component if
needed. right?

kndg said:
I does not seem possible even with Enum Class or at least I don't know how.


I can pass myenum.something but just could not find a way to pass myenum
where myenum is
enum myenum{something,whatever};
I am working with .net 3.5 sp1

why would I want enum paramater? I am working on a customizable component
and need the called to pass the complete enum to select available component
action
and ignore any additional items

yes there will have to be check about the parameter

Something like below?

public enum MyEnum { Something, Whatever };

public class MyClass
{
public static void Main(string[] args)
{
PrintEnumMembers(new MyEnum());
}

public static void PrintEnumMembers(Enum myEnum)
{
foreach (var item in Enum.GetNames(myEnum.GetType()))
{
Console.WriteLine(item);
}
}
}

But, I don't think that would be the right approach to your problem.

Regards.
 
K

kndg

thx

so I would be best to declare the enum at the global level as public scope.
this way, the enum can be overridden on invocation of the component if
needed. right?

Hi,

I'm not sure what do you mean by that, but generally, modifying an
already published public type IMO is a bad idea. Without enough details,
I cannot say for sure but you may try check the 'Command' design pattern.

Regards.
 
J

Jamal Samedov

Why you do not pass enumeration as a string and parse it during acceptance?


--

Dr. J.N. Samedov,

Software Manager
COINMASTERS B.V.
Adrianalaan 101a
3053 MA Rotterdam
The Netherlands

T: 00 31 (0)104186405
F: 00 31 (0)104222549
www.coin-masters.com
 
G

GS

thx

I will look into cmd design pattern.

as for the basic listbox component, ListBoxEnum. using enum itself, I have
built- in interface to allow datasource to bind to complex component from
the two arrays caller makes from enum for display and value members.

consequently the 2nd tier component using ListBoxEnum can make available
basic component processing avail and leave listbox item choices to the
caller of the 2nd tier component.
 

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