Passing an enum as a parameter

  • Thread starter Thread starter Simon
  • Start date Start date
S

Simon

Hi all,

I think I've seen someone passing an emumeration in code before.

Can anyone tell me if thats possible and why i would want to.

Many thanks

Kindest Regards

Simon
 
Hi Simon,

Do you mean to pass enum as a parameter to some method ?

Let's say you have this enum:

public enum DocumentType
{
invoice,
order
}

and some method that has DocumentType enum as a parameter:

public bool SaveDocument (string name, DocumentType docType)


Maybe this isn't a very good example but sure it is possible to pass an enum
to a method.
 
Peter said:
Hi Simon,

Do you mean to pass enum as a parameter to some method ?

Let's say you have this enum:

public enum DocumentType
{
invoice,
order
}

and some method that has DocumentType enum as a parameter:

public bool SaveDocument (string name, DocumentType docType)


Maybe this isn't a very good example but sure it is possible to pass an enum
to a method.

Unfortunately it is not possible to pass an enum to a method this way.
Since it is a special (user-defined) value type derived from
System.Enum, you need to pass the underlying type to the function.

For example, to make use of the enum's members you will have to pass
them seperately specifying their underlying type:

private enum MyEnum : int { ONE = 1, TWO = 2, THREE = 3 }

private void DoIt(int i)
{
switch (i)
{
case 1:
Console.WriteLine("MyEnum: {0}", i);
break;
case 2:
Console.WriteLine("MyEnum: {0}", i);
break;
case 3:
Console.WriteLine("MyEnum: {0}", i);
break;
default:
break;
}
}

this.DoIt((int)MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

This would print:

MyEnum: 1
MyEnum: 2
MyEnum: 3

Seems not very practical to me. Enums are simply not intended to be
passed to functions itself rather than their members using their
underlying type.

Maybe someone of the MVPs is able to shed some light on enums.

Regards

Catherine
 
Peter said:
Hi,

I think that the underlying type is integer by default

Hm, I am not sure about this, but consider the following example. It's a
slightly different version of the previous one.

private enum MyEnum { ONE = 1, TWO = 2, THREE = 3 }

private void PrintMyEnum(MyEnum e)
{
switch (e)
{
case MyEnum.ONE:
Console.WriteLine("MyEnum: {0}", e);
break;
case MyEnum.TWO:
Console.WriteLine("MyEnum: {0}", e);
break;
case MyEnum.THREE:
Console.WriteLine("MyEnum: {0}", e);
break;
default:
break;
}
}

this.PrintMyEnum(MyEnum.ONE);
this.PrintMyEnum(MyEnum.TWO);
this.PrintMyEnum(MyEnum.THREE);

This one prints:

MyEnum: ONE
MyEnum: TWO
MyEnum: THREE

So, what's the type now?

Regards

Catherine
 
Catherine Lowery wrote:

Unfortunately it is not possible to pass an enum to a method this
way. Since it is a special (user-defined) value type derived from
System.Enum, you need to pass the underlying type to the function.

Thats simply not true, there is absolutely no problem doing this at all.

using System;

namespace TestProject
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public enum MusicGenre {Rock,Punk,Pop,Country,Rap,Blues,Jazz}

class TestClass
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
TestClass test = new TestClass();
test.DisplayGenre(MusicGenre.Blues);
test.DisplayGenre(MusicGenre.Country);
test.DisplayGenre(MusicGenre.Jazz);
test.DisplayGenre(MusicGenre.Pop);
test.DisplayGenre(MusicGenre.Punk);
test.DisplayGenre(MusicGenre.Rap);
test.DisplayGenre(MusicGenre.Rock);
}

public void DisplayGenre(MusicGenre genre)
{
Console.WriteLine(genre.ToString());
}
}
}

Try that, works just fine.

Regards Tim.
 
Tim said:
Catherine Lowery wrote:





Thats simply not true, there is absolutely no problem doing this at all.

[snipped sample code for brevity]

You're right, so am I. Just as I stated previously you will need to pass
the enum's members to make use of the values assigned to them. I posted
a second sample to illustrate passing the enum itself. OK, I've got to
admit that I was not very clear. What I wanted to say is, that you need
to pass the members to make use of the value or the enum itself to make
use of the members as constants.

Cathetrine
 
Peter said:
Hi,

Since there is no underlying type declared the Int32 is used.

Thanks for the information. Just realized the same fact while testing. I
do not use enumerations very often since I am not very happy with the
concept. It's some kinda philosophical question with esotheric
tendencies :-)

Regards

Catherine
 
You don't need to pass the enum member to get the value assigned to them. If
you don't assign a value to enum member then the first member will have the
value 0 by default, the second member will have 1 and so on... and if you
set the values, well you will get the values you set :).

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
Catherine Lowery said:
Tim said:
Catherine Lowery wrote:




Thats simply not true, there is absolutely no problem doing this at all.

[snipped sample code for brevity]

You're right, so am I. Just as I stated previously you will need to pass
the enum's members to make use of the values assigned to them. I posted a
second sample to illustrate passing the enum itself. OK, I've got to admit
that I was not very clear. What I wanted to say is, that you need to pass
the members to make use of the value or the enum itself to make use of the
members as constants.

Cathetrine
 
Hmmm ... bits of confusion going on in this thread ...

There is no problem whatsoever in passing an enum type as a paremeter and often it is a better choice than say a boolean flag. Take the RemotingConfiguration.RegisterWellknownServiceType static method. Here is the signature ...

RemotingConfiguration.RegisterWellknownServiceType( Type typeToRemote, string uri, WellKnownObjectMode mode);

its that last param which is the interesting one - WellKnownObjectMode is defined as follows:

enum WellKnownObjectMode
{
Singleton
SingleCall
}

so only two values. So why didn't they use a boolean as the last parameter? Because the code is much more explicit using an enum - from looking at the method call you cal see gthe activation mode of the remote objectwithout having to check the docs to see what true and false mean.

By defaults enums are stored as 32 bit integers but you can change this using "inheritance like" syntax

enum WellKnownObjectMode : byte
{
Singleton
SingleCall
}

The above says 8 bits is enough to store the value.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Peter said:
Hi,

I think that the underlying type is integer by default

Hm, I am not sure about this, but consider the following example. It's a
slightly different version of the previous one.

private enum MyEnum { ONE = 1, TWO = 2, THREE = 3 }

private void PrintMyEnum(MyEnum e)
{
switch (e)
{
case MyEnum.ONE:
Console.WriteLine("MyEnum: {0}", e);
break;
case MyEnum.TWO:
Console.WriteLine("MyEnum: {0}", e);
break;
case MyEnum.THREE:
Console.WriteLine("MyEnum: {0}", e);
break;
default:
break;
}
}

this.PrintMyEnum(MyEnum.ONE);
this.PrintMyEnum(MyEnum.TWO);
this.PrintMyEnum(MyEnum.THREE);

This one prints:

MyEnum: ONE
MyEnum: TWO
MyEnum: THREE

So, what's the type now?

Regards

Catherine
 
this.PrintMyEnum(MyEnum.ONE);
this.PrintMyEnum(MyEnum.TWO);
this.PrintMyEnum(MyEnum.THREE);

This one prints:

MyEnum: ONE
MyEnum: TWO
MyEnum: THREE

So, what's the type now?

To be clear, while the enum is backed by an integer, the type of the
parameter is the enum itself.

However, yes, without a specification Int32 is the default backing value of
an enum.
 
Catherine said:
Tim said:
Catherine Lowery wrote:





Thats simply not true, there is absolutely no problem doing this at
all.

[snipped sample code for brevity]

You're right, so am I. Just as I stated previously you will need to
pass the enum's members to make use of the values assigned to them. I
posted a second sample to illustrate passing the enum itself. OK,
I've got to admit that I was not very clear. What I wanted to say is,
that you need to pass the members to make use of the value or the
enum itself to make use of the members as constants.

Cathetrine

I don't understand what you are saying here at all.

The original poster asked if he could pass an enum member as a
parameter to a function and Peter posted a reply and said yes and gave
a code example ? you then posted and said no you can't do that, and
this is what I don't get, How was Peters reponse wrong ? and What do
you mean that the underlying type of an enum needs to be passed to the
function ? this makes no sense to me.


Regards Tim.
 
Hi Simon,

There are two situations in which I use enum types as method parameters.

The first is a way to consolidate multiple properties which would be
boolean type for a custom class object. In this case, I use the enum
type with the [Flags] attribute and OR the options together:

[Flags]
public enum TheOptions
{
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8
}

static void Main(string args[])
{
// Set the options.
TheOptions myOptions =
TheOptions.One |
TheOptions.Three;

// Call the method with options.
MyMethod(myOptions);
}

static void MyMethod(TheOptions options)
{
if ((options & MyOptions.One) != 0)
{
// Option one is selected.
}

if ((options & MyOptions.Two) != 0)
{
// Option two is selected.
}
...
}

The other condition I use enums is for a multi-state switch where a
boolean type is insufficient.

public enum GetWhat
{
None = 0,
This = 1,
That = 2,
TheOther = 3
}

static void Main(string[] args)
{
// Call the method.
ThisMethod(GetWhat.TheOther);
}

static void ThisMethod(GetWhat getWhat)
{
switch getWhat
{
case GetWhat.This:
// This is selected.
break;
case GetWhat.That:
// That is selected.
break;
case GetWhat.TheOther:
// The other is selected.
break;
}
}

For more information, look up the FlagsAttribute Class and the Value
Type Usage Guidelines in the MSDN documentation. The latter will give
you some good ideas on enum usage.

Hope this helps.
- Glen
 
Simon... As an example, a class factory can take an enum to specify the
specific concrete class that needs to be created at runtime. In response
to
user input, the selected menu handler calls on the class factory and
passes
the appropriate enum as a parameter. The class factory then returns the
proper concrete class at runtime in response to the specific user
request. The
concrete class methods are invoked through polymorphic method calls so
that the caller does not need to know the actual concrete implementation
at
runtime, only that the concrete class implements the interface or base
class
virtual method.

http://www.geocities.com/jeff_louie/OOP/oop4.htm

Regards,
Jeff
I think I've seen someone passing an emumeration in code before.
Can anyone tell me if thats possible and why i would want to.<
 
Thank you everyone - I never realised people were so interested in the
common enum!

Thank you very much for all your advice.

Kindest Regards

Simon
 
Back
Top