By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

Enumeration Addition/Subtraction - I need an example, please

P: n/a
hi all,

After reading this excerpt from "The C# Programming Language", (By
Anders) I tried to check it out. Unfortunately, I'm getting compile
errors.

Can anyone illustrate this with an example:

----------------------------------------------------
Section 7.7.4 - enumeration addition
----------------------------------------------------
Every enumeration type implicitly provides the following predefined
operators, where E is the enum type and U is the underlying type of E.

E operator +(E x, U y);

E operator +(U x, E y);

The operators are evaluated exactly as (E)((U)x + (U)y).

Section 7.7.5 - enumeration subtraction
-----------------------------------------------------------
Every enumeration type implicitly provides the following predefined
operator, where E is the enum type and U is the underlying type of E.

U operator -(E x, E y);

This operator is evaluated exactly as (U)((U)x - (U)y). In other
words, the operator computes the difference between the ordinal values
of x and y, and the type of the result is the underlying type of the
enumeration.

E operator -(E x, U y);

This operator is evaluated exactly as (E)((U)x - y). In other words,
the operator subtracts a value from the underlying type of the
enumeration, yielding a value of the enumeration.
-----------------------------------------------------------
Thanks in advance

Dec 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"muler" <mu*************@gmail.comwrote:
>After reading this excerpt from "The C# Programming Language", (By
Anders) I tried to check it out. Unfortunately, I'm getting compile
errors. Can anyone illustrate this with an example:
I'm not really sure what you're asking. Here's an example that
exercises all four of the operators you posted. The underlying
datatype of the enumeration is an int.

enum Days {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday ,Sunday};

static void Main(string[] args)
{ Days d = Days.Tuesday;
Days e = d+1; // e==Wednesday
Days f = 2+e; // f==Friday
int diff = f-d; // f==3, i.e. Friday-Teusday
Days g = f-3; // g==Tuesday
}

--
Lucian
Dec 12 '06 #2

P: n/a
Hi,

Thanks for your response. Now, i noted that the addition and
subtraction are valid for elements of the same enumeration. I was
trying something like this:

namespace EnumAdd
{
enum X
{
i,
j,
k = 10,
l
}

enum Y
{
m,
n = 40,
o,
p
}

class Program
{
static void Main(string[] args)
{
X e1 = X.l;
Y e2 = Y.p;

// compile error:
// Operator '-' cannot be applied to operands of type
'EnumAdd.X' and 'EnumAdd.Y'
int k = e1 - e2;
}
}
}

Thanks.

Lucian Wischik wrote:
"muler" <mu*************@gmail.comwrote:
After reading this excerpt from "The C# Programming Language", (By
Anders) I tried to check it out. Unfortunately, I'm getting compile
errors. Can anyone illustrate this with an example:

I'm not really sure what you're asking. Here's an example that
exercises all four of the operators you posted. The underlying
datatype of the enumeration is an int.

enum Days {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday ,Sunday};

static void Main(string[] args)
{ Days d = Days.Tuesday;
Days e = d+1; // e==Wednesday
Days f = 2+e; // f==Friday
int diff = f-d; // f==3, i.e. Friday-Teusday
Days g = f-3; // g==Tuesday
}

--
Lucian
Dec 12 '06 #3

P: n/a
muler wrote:
Hi,

Thanks for your response. Now, i noted that the addition and
subtraction are valid for elements of the same enumeration. I was
trying something like this:
// compile error:
// Operator '-' cannot be applied to operands of type
'EnumAdd.X' and 'EnumAdd.Y'
int k = e1 - e2;
Naturally. Just as the reference you quoted states:

<quote>
Section 7.7.5 - enumeration subtraction
-----------------------------------------------------------
Every enumeration type implicitly provides the following predefined
operator, where E is the enum type and U is the underlying type of E.

U operator -(E x, E y);
</quote>

note that the two operands of operator - are of the same type E. You're
trying to use two different types. To subtract enum values from seperate
enumerations, you have to explicitly cast them to a compatible integer type
first.

int k = (int)e1 - (int)e2;

-cd

Dec 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.