q: enum as a param

  • Thread starter Thread starter dllhell
  • Start date Start date
D

dllhell

hi all,

I have a problem with creating proc with a enum as a param.
I wish to pass an enumeration in proc in which one I intend to do some
operations with enumeration, but I don't know which one enumeration I'll
pass.

so... I have tried with:
string[] SomeResult (enum** _enum) { }

and I get 4 errors:
Type expected
Class, struct, or interface method must have a return type
Type or namespace definition, or end-of-file expected

How to write proc enum parameter?
Sorry for my English and thanks in advance.
__________________________
**code snippet
 
"dllhell" <[email protected]> a écrit dans le message de [email protected]...

| so... I have tried with:
| string[] SomeResult (enum** _enum) { }
|
| and I get 4 errors:
| Type expected
| Class, struct, or interface method must have a return type
| Type or namespace definition, or end-of-file expected
|
| How to write proc enum parameter?

public class MyClass
{
string[] SomeResult (Enum _enum)
{
// your code
}
}

Joanna
 
You can pass the enum as type object and then cast it.
You cannot use the basetypes System.Enum or System.ValueType directly.
 
What exactly doesn't work?
BTW what language do you use? The code that you wrote with this "enum**
_enum" makes me thing that you might use C++.

Anyways I believe you should give more information on what is that you want
to do with the _enum inside the method body.

The suggestion Joanna gave you is a working one and this is a way that you
can passany kind enumeration value to a method as a parameter, you probably
don't use the _enum correctly inside the method.


--

Stoitcho Goutsev (100)

dllhell said:
public class MyClass
{
string[] SomeResult (Enum _enum)
{
// your code
}
}

No difference. Doesn't work.
 
Stoitcho Goutsev (100) said:
What exactly doesn't work?

In my first post I have list errors.
However... Try it by yourself it is really one line of code.
BTW what language do you use? The code that you wrote with this "enum**
_enum" makes me thing that you might use C++.
c#

i have used enum** as a mark for footnote to avoid misanderstanding
(enum/Enum)
You can also find it on botom of my first post:
__________________________
**code snippet

Anyways I believe you should give more information on what is that you
want to do with the _enum inside the method body.

My procedure is a function which have to return array:
string[] names = Enum.GetNames(typeof(_Enum));

and I dont know which enum will be parsed into function
The suggestion Joanna gave you is a working one and this is a way that you
can passany kind enumeration value to a method as a parameter, you
probably don't use the _enum correctly inside the method.

so please instruct me how to do so
Joanna can't see difference between Enum and enum
 
Hi,


dllhell said:
public class MyClass
{
string[] SomeResult (Enum _enum)
{
// your code
}
}

No difference. Doesn't work.

Can you post the error you are getting?

This code works for me:

public enum MyEnum { valu1, valu2 };

void Method1( MyEnum e)
{
}

Also, the error you are getting, it's at runtime or at compile time?
 
"dllhell" <[email protected]> a écrit dans le message de [email protected]...

| so please instruct me how to do so
| Joanna can't see difference between Enum and enum

Oh yes I can, "Enum" is the base class for all enums, "enum" is a reserved
word in C#.

You need to check the syntax of what you are writing :-)

public class MyClass
{
string[] GetEnumNames(Enum e)
{
return Enum.GetNames(e.GetType());
}
}

Also, this is a pretty useless function as all you have to do is call :

{
string[] names = Enum.GetNames(e.GetType());
..
}

Joanna
 
Hi,


dllhell said:
Greg Young said:
string [] SomeResult(SomeEnum _Value) {
}

hehe
If I know which enum I'll get in procedure I wouldn't bother group with so
stupid question :)

I also missed that part :)

If you do not know the enum type you can use reflection to do certain
operations ( GetValues/Names, etc ) like this:


enum e1 { v,b,d};

static void method( Type t)
{
foreach( string s in Enum.GetNames( t ) )
Console.WriteLine( s);

foreach( string s in Enum.GetValues( t ) )
Console.WriteLine( s);
}


You call the method like this:

//do not have an instance
method( typeof(e1));

//with an isntance
e1 var1;
var1 = e1.b;
method( var1.GetType() );
 
class Program
{
static void Main(string[] args) {
new Flub().FlubFunction(Machine.Wrapper);
new Flub().FlubFunction(Chocolate.Milk);
Console.ReadKey();
}
}

public enum Machine { Boxer, Wrapper, Mixer }
public enum Chocolate { Milk, Dark, White }

public class Flub
{
public void FlubFunction(object enumValue) {
if (enumValue is Machine) {
Console.WriteLine("is Machine");
// ...
}

if (enumValue is Chocolate) {
Console.WriteLine("is Chocolate");
// ...
}
}
}


You could also use "as" and then test for null. In fact "as" is more
efficient in that it only casts once.
--

Andrew Robinson
http://blog.binaryocean.com


dllhell said:
Greg Young said:
string [] SomeResult(SomeEnum _Value) {
}

hehe
If I know which enum I'll get in procedure I wouldn't bother group with so
stupid question :)
 
static void Main(string[] args)
{

foreach (string str in Bar(Foo.one))
{
Console.Write(str);
Console.Write(";");
}
Console.WriteLine();
foreach (string str in Bar(FooFoo.three))
{
Console.Write(str);
Console.Write(";");
}
Console.ReadLine();
}

static string[] Bar(Enum par)
{
return Enum.GetNames(par.GetType());
}

}

enum Foo
{
one,
two
}

enum FooFoo
{
three,
four
}


Watch out for your casing - enum and Enum are different things as well as
_enum and _Enum.

enum is a keyword in C# it cannot be used as data type. Enum on the other
hand is a type and is base type for all enumerations.

_enum is your method parameter where _Enum is undefined in this case.


--
HTH
Stoitcho Goutsev (100)
dllhell said:
Stoitcho Goutsev (100) said:
What exactly doesn't work?

In my first post I have list errors.
However... Try it by yourself it is really one line of code.
BTW what language do you use? The code that you wrote with this "enum**
_enum" makes me thing that you might use C++.
c#

i have used enum** as a mark for footnote to avoid misanderstanding
(enum/Enum)
You can also find it on botom of my first post:
__________________________
**code snippet

Anyways I believe you should give more information on what is that you
want to do with the _enum inside the method body.

My procedure is a function which have to return array:
string[] names = Enum.GetNames(typeof(_Enum));

and I dont know which enum will be parsed into function
The suggestion Joanna gave you is a working one and this is a way that
you can passany kind enumeration value to a method as a parameter, you
probably don't use the _enum correctly inside the method.

so please instruct me how to do so
Joanna can't see difference between Enum and enum
 
Joanna Carter said:
"dllhell" <[email protected]> a écrit dans le message de [email protected]...

| so please instruct me how to do so
| Joanna can't see difference between Enum and enum

Oh yes I can, "Enum" is the base class for all enums, "enum" is a reserved
word in C#.

You need to check the syntax of what you are writing :-)

public class MyClass
{
string[] GetEnumNames(Enum e)
{
return Enum.GetNames(e.GetType());
}
}

ok, pls show me how you call this procedure.
 
dllhell said:
public class MyClass
{
string[] GetEnumNames(Enum e)
{
return Enum.GetNames(e.GetType());
}
}
ok, pls show me how you call this procedure.

enum MyFirstEnum {monday, tuesday, wednesday};
enum OtherEnum {Hot, Cold, Lukewarm};

static void Main(string[] args)
{ string[] x = GetEnumNames(MyFirstEnum.monday);
string[] y = GetEnumNames(OtherEnum.Hot);
}
 
enum MyFirstEnum {monday, tuesday, wednesday};
enum OtherEnum {Hot, Cold, Lukewarm};

static void Main(string[] args)
{ string[] x = GetEnumNames(MyFirstEnum.monday);
string[] y = GetEnumNames(OtherEnum.Hot);
}

And what I suppose to do with that? :))
anyway... Ignacio illuminates me
 
dllhell said:
enum MyFirstEnum {monday, tuesday, wednesday};
enum OtherEnum {Hot, Cold, Lukewarm};

static void Main(string[] args)
{ string[] x = GetEnumNames(MyFirstEnum.monday);
string[] y = GetEnumNames(OtherEnum.Hot);
}

And what I suppose to do with that? :))

I think you should learn how to ask your questions better! You asked:
"I wish to pass an enumeration in proc in which one I intend to do
some operations with enumeration, but I don't know which one
enumeration I'll pass." So here your question has been answered: a
function which accepts an enumeration, and it works with any
enumeration that you pass.
 
"dllhell" <[email protected]> a écrit dans le message de [email protected]...

| And what I suppose to do with that? :))

Be grateful that people took time out of their busy working days to try and
help you !!

The answers Lucian, I and others contributed were all good, all you were
doing wrong was to try and call typeof(x) instead of x.GetType().

Enum.GetNames(...) returns an array of string ( string[] ). The only
difference between our and Ignacio's examples was that Ignacio called an
enumerator on the array returned from GetNames(...) without first assigning
it to a variable.

I take it you are too lazy to experiment yourself and simply wanted someone
to guess what you really wanted and write the code for you ?

Joanna
 
Back
Top