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

Generics and comparison (follow on from my previous "Generics and collections" thread)

Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ...

public T Value {
get { return this.value; }
set {
if ( this.value != value ) {
this.value = value;
this.OnValueChanged(EventArgs.Empty);
}
}
}

This code produces the Operator '==' cannot be applied to operands of type 'T' and 'T' error at compile time!
I am sure that I am not the only person to have tried this, any clues as to the correct work around?

I have tried restricting 'T' to IComparable in a where clause and then casting 'this.value' and 'value' to IComparable for the comparison - the code then compiles but is this the right answer? It feels wrong, but I am quickly learning that my own understanding of generics is much lower than I thought it was!

Thanks in advance.

Apr 4 '08 #1
2 1120
Though it compiles, the original IComparable idea did not work; the comparisons would often return incorrect results.

I have resolved it for now using "if ( this.value == null ? value != null : !this.value.Equals(value) )" but I am still open to a better way if it exists ...

Martin.

"Martin Robins" <martin at orpheus-solutions dot co dot ukwrote in message news:uj*************@TK2MSFTNGP06.phx.gbl...
Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ...

public T Value {
get { return this.value; }
set {
if ( this.value != value ) {
this.value = value;
this.OnValueChanged(EventArgs.Empty);
}
}
}

This code produces the Operator '==' cannot be applied to operands of type 'T' and 'T' error at compile time!
I am sure that I am not the only person to have tried this, any clues as to the correct work around?

I have tried restricting 'T' to IComparable in a where clause and then casting 'this.value' and 'value' to IComparable for the comparison - the code then compiles but is this the right answer? It feels wrong, but I am quickly learning that my own understanding of generics is much lower than I thought it was!

Thanks in advance.

Apr 4 '08 #2
Martin Robins wrote:
Though it compiles, the original IComparable idea did not work; the
comparisons would often return incorrect results.
That is a problem with the implementation of the interface for that
particular instantiation of T, then. If it were correctly implemented,
that should not happen.
--
Rudy Velthuis http://rvelthuis.de

"Sailors ought never to go to church. They ought to go to hell,
where it is much more comfortable." -- HG Wells.
Apr 4 '08 #3

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

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
11
by: Brian Mitchell | last post by:
Hello, I am using several queue collections to store some data (about 20,000 entries split across 2 collections). When I view the process in the Windows task manager I can see the memory increasing...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
3
by: Steve Foster | last post by:
I may be missing something simple, but I cannot find any way to get a collection of managed threads in a process. What am I missing? Steve (P.S. When I say managed, I mean...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
2
by: semedao | last post by:
Hi, what is the best way to Synchronized threads on classes that use or inherit from Generics ? (Dictionary for example) Thanks. (and where is the *Class*.Synchronized method for them ?)
18
by: beginner | last post by:
Hi All. I'd like to do the following in more succint code: if k in b: a=b else: a={} b=a
2
by: Martin Robins | last post by:
Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ... public T Value { get {...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
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
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
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...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.