Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Compare two integer constants

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amir Tohidi - 12 Oct 2007 09:28 GMT
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
Paul E Collins - 12 Oct 2007 09:40 GMT
> I have two constant integers and when I try to compare
> their values, I get error:
[quoted text clipped - 9 lines]
>
> 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.
Amir Tohidi - 12 Oct 2007 09:47 GMT
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;
}

> Hi
>
[quoted text clipped - 21 lines]
>
> Thanks
Jon Skeet [C# MVP] - 12 Oct 2007 09:52 GMT
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
Ben Voigt [C++ MVP] - 12 Oct 2007 18:57 GMT
> On Oct 12, 9:47 am, Amir Tohidi <AmirToh...@discussions.microsoft.com>
> wrote:
[quoted text clipped - 3 lines]
>
> No - I'd use an enum instead.

An enum isn't extensible though... sometimes the struct with a single
integer field is better.

> Jon
Jon Skeet [C# MVP] - 12 Oct 2007 19:04 GMT
> > 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.)

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.