473,739 Members | 7,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET 2.0 generic.list.In dexOf doesn't call CompareTo of contained class


I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collecti ons.Generic.Lis t appears to
be the ideal solution. However, the generic.List.In dexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.

The contained class (IssStruct) implements the IComparable and
IComparable<T> interfaces.

However, the List.Sort function does invoke CompareTo, which suggests
that the syntax for the IComparable interface is correct. The same
problem occurs when the class in contained in an ArrayList.

I'm a rank beginner, so feel free to point out anything obvious.

Below are the class declarations. IssStructs is a generic list that
contains IssStruct class instances; UnsafeIssStruct s is an ArrayList
that contains IssStruct class instances. They behave the same. IndexOf
does not invoke IssStruct.Compa reTo, Sort does invoke
IssStruct.Compa reTo.

Muchos thanks to anyone who can offer help.

namespace ClassLibrary1
{

// this is the contained class
public class IssStruct:IComp arable<IssStruc t>, IComparable
{
public string fStructName;

public IssStruct(strin g iStructName)
{
fStructName = iStructName;
}

// non-generic version of CompareTo
public int CompareTo(objec t obj)
{
return fStructName.Com pareTo(((IssStr uct)obj).fStruc tName);
}
// generic version of CompareTo
public int CompareTo(IssSt ruct obj)
{
return fStructName.Com pareTo(obj.fStr uctName);
}
} // end IssStruct, the contained class

// container class. "Has a..." list, not an "Is a..." list
public class IssStructList
{
public List<IssStruct> IssStructs;
public ArrayList UnSafeIssStruct s;
public IssStructList()
{
IssStructs = new List<IssStruct> ();
UnSafeIssStruct s = new ArrayList();
}
}
} // end namespace ClassLibrary1

Nov 17 '05 #1
5 5442
On 5 Aug 2005 15:06:56 -0700, "majm" <ba********@ear thlink.net> wrote:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collecti ons.Generic.Lis t appears to
be the ideal solution. However, the generic.List.In dexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understandin g is that it should.


No, it should and does invoke Equals (inherited from Object if not
defined on your element type). IndexOf only needs equality, not
ordering, hence Equals rather than CompareTo.

If you want to do a search that uses the ordering established by
CompareTo you need to call first Sort, then BinarySearch, rather than
IndexOf. BinarySearch will invoke CompareTo to find an element.
--
http://www.kynosarges.de
Nov 17 '05 #2

"Christoph Nahr" <ch************ @kynosarges.de> wrote in message
news:er******** *************** *********@4ax.c om...
On 5 Aug 2005 15:06:56 -0700, "majm" <ba********@ear thlink.net> wrote:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collecti ons.Generic.Lis t appears to
be the ideal solution. However, the generic.List.In dexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understandi ng is that it should.


No, it should and does invoke Equals (inherited from Object if not
defined on your element type). IndexOf only needs equality, not
ordering, hence Equals rather than CompareTo.

If you want to do a search that uses the ordering established by
CompareTo you need to call first Sort, then BinarySearch, rather than
IndexOf. BinarySearch will invoke CompareTo to find an element.


The docs, however, say that IndexOf uses CompareTo. I've fired off an email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable
You've got me. What version of MSDN help are you using?
Nov 17 '05 #3
On Sat, 6 Aug 2005 04:16:38 -0500, "Daniel O'Connell [C# MVP]"
<onyxkirx@--NOSPAM--comcast.net> wrote:
The docs, however, say that IndexOf uses CompareTo. I've fired off an email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable


Now that you mention it, I do recall that there was such an error in
the Collections docs in one beta version. It's fixed in the current
July CTP, though.

IEquatable defaults to Object.Equals, by the way, although explicitly
implementing the interface should provide better performance for value
types I guess.
--
http://www.kynosarges.de
Nov 17 '05 #4

"Christoph Nahr" <ch************ @kynosarges.de> wrote in message
news:qv******** *************** *********@4ax.c om...
On Sat, 6 Aug 2005 04:16:38 -0500, "Daniel O'Connell [C# MVP]"
<onyxkirx@--NOSPAM--comcast.net> wrote:
The docs, however, say that IndexOf uses CompareTo. I've fired off an
email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable
Now that you mention it, I do recall that there was such an error in
the Collections docs in one beta version. It's fixed in the current
July CTP, though.


Ya,that appears to be the case. I'm still using beta2 due to my extreme
laziness.

IEquatable defaults to Object.Equals, by the way, although explicitly
implementing the interface should provide better performance for value
types I guess.


Ya, it does seem a little superflous at points, but an examination of the
class shows that that is what it does. I don't recall having ever heard of
it, somehow.
Nov 17 '05 #5
Thanks, gentlemen. IEquatable did it.

Nov 17 '05 #6

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

Similar topics

1
2283
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None, ascending = True): ' Sort lists or dictionaries by the specified key' t = type(container)
18
4743
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
2
1977
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of "transform" which works not only on lists, but lists of list, lists of lists of lists, etc. I'm calling it "fmap" and it passes around actual lists instead of iterators (for now). In order to get it to work, I thought I'd have one templated...
6
5642
by: SharpCoderMP | last post by:
hi, until .net 2.0 i've been writing my own collections derived from CollectionBase. Now with the generics it is much easier to do that... but! i have some questions about generic lists of complex objects. consider this short example: class MyComplexClass : IDisposable {
4
3143
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my question In short my question is why when I pass a generic type directly to the formatObject function it works fine, but when I pass it to the checkText function where it is itself a generic argument, and then it is passed to formatObject, it is seen...
3
2275
by: Peter Olcott | last post by:
How does not specify the sort criteria for Generic.List ?? The way that this is done in C++ STL is to implement operator<(), how is this done in C# and DotNet for Generic.List ???
1
3903
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
0
13315
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the
5
4189
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I want to pass nested collections generically:
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8794
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
9342
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...
0
9211
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
8218
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
6056
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.