473,837 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4800
"muler" <mu************ *@gmail.comwrot e:
>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,Thur sday,Friday,Sat urday,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.comwrot e:
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,Thur sday,Friday,Sat urday,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
12634
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 value="red"/> <xsd:enumeration value="yellow"/> <xsd:enumeration value="blue"/> </xsd:simpleType>
2
2572
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 enumeration, but just a subset. Why is this? Paul.
34
16705
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 - 3;
25
521
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, this should print 1 (true) but it prints 0! If I modify this to d = c < (a - b);
4
5665
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" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly convenient, is also one of the most gross warping of language concepts to date... public IEnumerator...
3
10631
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 after. Create a class named “clsMyItems” and in that class place an enum. Public Enum Items Item1 = 0 Item2 = 1 Item3 = 2
5
2288
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 { iv = iv + box.value
6
2160
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
20
2724
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 work on all implementation. size_t size_obj = (char*)(&obj + 1) - (char*)(&obj); I wanted to find out on which implementation this would fail. One of
0
9685
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10886
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10580
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10277
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9413
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
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 we have to send another system
2
4052
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.