473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum base class

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
Sep 3 '07 #1
4 2135
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:

JBHi all!

JBI have two enumerations:

JBpublic enum MyEnum1 and public enum MyEnum2

JBI want to create function that accepts parameter as member of either
JBenumeration. Is this at all possible?
JBSomething like:
JBpublic void MyFunc(EnumBase myEnumValue)
JB{
JB}

JBOr is there some other way to accomplish this. Actually I have
JBarround 10 enums and I don't want to create separate functions
JBsince the logic is the same?
JBMaybe I can accomplish something through System.Enum?
JBBest regards,
JB Jure


Sep 3 '07 #2
"Jure Bogataj" <ju**********@mikrocop.comwrote in message
news:eB**************@TK2MSFTNGP06.phx.gbl...
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?

Sep 3 '07 #3
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

"Doug Semler" <do********@gmail.comwrote in message
news:uX**************@TK2MSFTNGP03.phx.gbl...
"Jure Bogataj" <ju**********@mikrocop.comwrote in message
news:eB**************@TK2MSFTNGP06.phx.gbl...
>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?

Sep 4 '07 #4
On Sep 4, 3:04 am, "Jure Bogataj" <jure.boga...@mikrocop.comwrote:
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
}

Sep 4 '07 #5

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

Similar topics

2
2799
by: BillyO | last post by:
I need a general mechanism for passing parameters to an API function I am designing, call it foo. Foo processes data, the operation used to process the data and the parameters for that operation...
2
7407
by: Nobody | last post by:
Hi All, In my application I have the following implementation for ClassA and ClassC invoking a call on ClassB. void ClassA::DoSomething() { int value = m_pClassB->Read(enum1); }
3
1767
by: Matteo Settenvini | last post by:
Mmmh... I was trying some code, and I bumped in a strange behaviour. The following piece of code compiles normally : //////// CODE STARTS ///////////////// #include <iostream> class B {...
31
3568
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
3
11169
by: ssg31415926 | last post by:
I have an abstract base class with an enum which is passed into a method. I want my derived classes to be able to add new values to the enum without having to redefine it and I want to be able to...
13
12361
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
4
2409
by: Coder | last post by:
Hi, Running into an issue. I am creating a base class. This base class has a "State" property of type "States" which is a locally defined enum type. The issue is this...the derived class...
7
9807
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
2
1213
by: RJ | last post by:
I was hoping to code a base class that would somehow require inheriting classes to define an Enum. Each inheriting child class needs to define it's own enum with it's own enumerated values, which...
34
11133
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
7260
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
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5086
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...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.