473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Enumeration Addition/Subtraction - I need an example, please

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
3 4759
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
2
by: PaulUK | last post by:
When I use the following C# code: string colorNames = Enum.GetNames(typeof(KnownColor)); colorNames does not contain the full list of member names detailed in the System.Drawing.KnownColor...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
25
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1). Now,...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
5
by: Paul | last post by:
Why is the addition here adding the number as a string but the subtraction works fine? function boxchange(box) { iv= dbform.ut.value if (box.checked == true) { iv = iv - box.value } else {...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
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...
0
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...

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.