enum base class

J

Jure Bogataj

Hi all!

I have two enumerations:

public enum MyEnum1
and
public enum MyEnum2

I want to create function that accepts parameter as member of either
enumeration. Is this at all possible?
Something like:
public void MyFunc(EnumBase myEnumValue)
{
}

Or is there some other way to accomplish this. Actually I have arround 10
enums and I don't want to create separate functions since the logic is the
same?
Maybe I can accomplish something through System.Enum?


Best regards,
Jure
 
V

Vadym Stetsiak

Hello, Jure!

If your enums do not derive from other types then int, you can cast all of
them to int.

public enum E1
{
val1,
val2,
}
public static MyFunc(int enumValue)
{
if ( E1.val1 == (E1)enumValue )
{
//do something
}
}

MyFunc((int)E1.val1);

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Mon, 3 Sep 2007 16:21:18 +0200:

JB> Hi all!

JB> I have two enumerations:

JB> public enum MyEnum1 and public enum MyEnum2

JB> I want to create function that accepts parameter as member of either
JB> enumeration. Is this at all possible?
JB> Something like:
JB> public void MyFunc(EnumBase myEnumValue)
JB> {
JB> }

JB> Or is there some other way to accomplish this. Actually I have
JB> arround 10 enums and I don't want to create separate functions
JB> since the logic is the same?
JB> Maybe I can accomplish something through System.Enum?


JB> Best regards,
JB> Jure
 
D

Doug Semler

Jure Bogataj said:
Hi all!

I have two enumerations:

public enum MyEnum1
and
public enum MyEnum2

I want to create function that accepts parameter as member of either
enumeration. Is this at all possible?
Something like:
public void MyFunc(EnumBase myEnumValue)
{
}

Or is there some other way to accomplish this. Actually I have arround 10
enums and I don't want to create separate functions since the logic is the
same?
Maybe I can accomplish something through System.Enum?


You could cast all of the enums to int and declare the function with the int
parameter.

However, I question a design that requires 10 different enumerations which,
when cast, provide the same values for a particular functionality. If
you're not examining the bits but need to keep the type, you can always pass
it to your function as an object:

void myFunc(object o)
{
Console.WriteLine(Enum.Format(o.GetType(), o, "x"));
}
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
J

Jure Bogataj

Actually, members of the enumeration are declared so only one bit of 32-bit
unsigned int is set.

e.g.

public enum MyEnum1
{
enumValue1 = 0x00000001,
enumValue2 = 0x00000002,
enumValue3 = 0x00000004,
enumValue4 = 0x00000008,
enumValue5 = 0x00000010,
enumValue6 = 0x00000020,
}

Then I check all enumeration / their values the same way. I just have many
enumerations for code clarity?

Best regards,
Jure
 
D

Doug Semler

Actually, members of the enumeration are declared so only one bit of 32-bit
unsigned int is set.

e.g.

public enum MyEnum1
{
enumValue1 = 0x00000001,
enumValue2 = 0x00000002,
enumValue3 = 0x00000004,
enumValue4 = 0x00000008,
enumValue5 = 0x00000010,
enumValue6 = 0x00000020,

}

Then I check all enumeration / their values the same way. I just have many
enumerations for code clarity?

Best regards,
Jure

First, if you are defining enum values this way, you should attribute
the enum with the [FlagsAttribute]. If not, there is no reason to
define the individual bit values.

What do you mean "check their values" the same way? Enumerations are
meant to be logical groupings. There is no real reason to have two
enumerations have the same semantic meaning (2 exceptions, converting
from an internal interface to a public external interface, or maybe 2
different versions for backward compatibility). It would be
confusing to me as a programmer to run across ten different
enumerations that behaved exactly the same way.


Do you mean that you have a function that does something like "Checks
to see if bit N is set"? (e.g.:

bool IsBitNSet(int bitfield, int bitnum)
{
return (bitfield & (1 << (bitnum - 1))) != 0;
// or
return (bitfield & bitnum) != 0;
}

and then in your code you have:

if (IsBitNSet((int)myEnumVal, 1)) // or IsBitNSet((int)myEnumVal,
0x010111)
{
// Do work.
}

I maintain it is easier to understand the programmer's intent if you
bypass the function call conversions and/or direct bit masking:

MyEnum bitsToCheck = MyEnum.enumValue1 | MyEnum.enumValue4;
if ((myEnumVal & bitsToCheck) != MyEnum.None) // For flags, add a
"None = 0" value.
{
// Do work
}
 
S

sloan

Maybe this sample can help:



using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Reflection;

using System.Xml;

namespace MyCompany.MyApplication

{

public class EnumHelper

{

private EnumHelper()

{

//only static methods

}

public static string GetDescription(System.Enum value)

{

//if a description is defined for an enum value, this procedure will get it

//Example of defining a description with an enum value

/*

*

public enum ExampleEnum

{

[Description("MyUnknownDescription")] Unknown = 0

}



*

* */

FieldInfo fi = value.GetType().GetField(value.ToString());

DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),
false);

return (attributes.Length > 0) ? attributes[0].Description :
value.ToString();

}

public static int[] GetSelectedIds(string idXml, string elementName)

{

int[] ids = null;

try

{

if (!idXml.Equals(string.Empty))

{

XmlDocument doc = new XmlDocument();

doc.LoadXml(idXml);

XmlNodeList nodes = doc.GetElementsByTagName(elementName);

ids = new int[nodes.Count];

int idx = 0;

foreach (XmlNode element in nodes)

{

ids.SetValue(Convert.ToInt32(element.InnerText), idx);

idx++;

}

}

}

finally

{

}

return ids;

}

}

}
 

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