473,396 Members | 1,895 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,396 software developers and data experts.

Compare two integer constants

Hi

I have two constant integers and when I try to compare their values, I get
error:

Operator '==' cannot be applied to operands of type ...

Here is a cut down version of my code

public class MY_CONSTANTS
{
public const int FOO = 1;
...
}

MY_CONSTANTS m_value = MY_CONSTANTS.FOO;

public bool Bar(MY_CONSTANTS val)
{
return m_value == val; // Errors.
}

Is there a way around this?

Thanks
Oct 12 '07 #1
5 1844
"Amir Tohidi" <Am********@discussions.microsoft.comwrote:
I have two constant integers and when I try to compare
their values, I get error:
Operator '==' cannot be applied to operands of type ...

Here is a cut down version of my code

public class MY_CONSTANTS
{
public const int FOO = 1;
...
}

MY_CONSTANTS m_value = MY_CONSTANTS.FOO;
MY_CONSTANTS.FOO is not a MY_CONSTANTS instance. It is an int, so you
would have to make m_value an int as well.

Alternatively, make MY_CONSTANTS an enum instead of a class.

Eq.
Oct 12 '07 #2
Hi

I managed to get around the problem using a struct and implicit operator. Is
this the right approach?

public struct MY_CONSTANTS // changed to struct
{
public const int FOO = 1;
...

public MY_CONSTANTS(MY_CONSTANTS value)
{
m_value = Convert.ToInt32(value);
}

public static implicit operator int(MY_CONSTANTS c)
{
return Convert.ToInt32(c.m_value);
}

private int m_value;
}
"Amir Tohidi" wrote:
Hi

I have two constant integers and when I try to compare their values, I get
error:

Operator '==' cannot be applied to operands of type ...

Here is a cut down version of my code

public class MY_CONSTANTS
{
public const int FOO = 1;
...
}

MY_CONSTANTS m_value = MY_CONSTANTS.FOO;

public bool Bar(MY_CONSTANTS val)
{
return m_value == val; // Errors.
}

Is there a way around this?

Thanks
Oct 12 '07 #3
On Oct 12, 9:47 am, Amir Tohidi <AmirToh...@discussions.microsoft.com>
wrote:
I managed to get around the problem using a struct and implicit operator. Is
this the right approach?
No - I'd use an enum instead.

Jon

Oct 12 '07 #4

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@q5g2000prf.googlegro ups.com...
On Oct 12, 9:47 am, Amir Tohidi <AmirToh...@discussions.microsoft.com>
wrote:
>I managed to get around the problem using a struct and implicit operator.
Is
this the right approach?

No - I'd use an enum instead.
An enum isn't extensible though... sometimes the struct with a single
integer field is better.
>
Jon
Oct 12 '07 #5
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
No - I'd use an enum instead.

An enum isn't extensible though... sometimes the struct with a single
integer field is better.
Occasionally - but in that case I'd use an alternative enum-like
pattern such that the integer value was hidden, or expose it via a
property. Implicit conversions are almost always a bad idea, IMO. (In
fact, just today I fixed a bug due to a conversion. It was very hard to
track down because it just wasn't obvious what was going on when you
looked at the C# code. The IL made it a lot clearer.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '07 #6

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

Similar topics

29
by: SysSpider | last post by:
Hi again, This is my problem: when i try to compile the code that contains the function below, i get this: -- gcc:21: error: case label does not reduce to an integer constant gcc:24: error:...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
7
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I...
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
9
by: Fred | last post by:
I'm having terrible trouble trying to work out exactly which of the promotions one reads about in old books are still present in current C - and there seems to be a distinction between promotion...
5
by: Rex Mottram | last post by:
Can anyone explain this? % cat t.c #include <stdio.h> #define FIRST "first" #define SECOND "second" int main(int argc, char *argv)
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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,...

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.