Enumeration Addition/Subtraction - I need an example, please

M

muler

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.
 
L

Lucian Wischik

muler said:
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
}
 
M

muler

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 said:
muler said:
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
}
 
C

Carl Daniel [VC++ MVP]

muler said:
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
 

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