473,550 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CompareTo on base class not sorting children

i wonder if anyone can help...

i have an abstract class called "Message" that impliment's icompareable
on the property "TimeSent".
i have two types of messages that inherit the "Message" class - Timed
Messages and Questions.
i get some of both and put them into an array list and then use the
Sort method. I need it to sort them by TimeSent regardless of type but
it groups them into questions and timedmessages. i could have sworn i
got this working at one point or maybe i just wished i had.
i thought that since they both inherit "Message" and "Message"
imliements icompareable it would sort them all as messages. the code
i'm using in "Message" is:

public int CompareTo(objec t obj)
{
if(obj is Message)
{
Message temp = (Message) obj;

return timeSent.Compar eTo(temp.TimeSe nt);
}
else
{
return 0;
}
}

if anyone can help me with this i'd be grateful. cheers!

Dec 9 '05 #1
3 2180
Hi,
Are you sure that both classes initialize the DateTime ( I assume TimeSent
is a DateTime ) , maybe one of them does not and it gets the default value.

Did you debug it , let's say an array with 4 elements, 2 of each type and
see what happen ? It should be fairly simple to follow the execution.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Charlie Bear" <ch*****@contra positive.tv> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
i wonder if anyone can help...

i have an abstract class called "Message" that impliment's icompareable
on the property "TimeSent".
i have two types of messages that inherit the "Message" class - Timed
Messages and Questions.
i get some of both and put them into an array list and then use the
Sort method. I need it to sort them by TimeSent regardless of type but
it groups them into questions and timedmessages. i could have sworn i
got this working at one point or maybe i just wished i had.
i thought that since they both inherit "Message" and "Message"
imliements icompareable it would sort them all as messages. the code
i'm using in "Message" is:

public int CompareTo(objec t obj)
{
if(obj is Message)
{
Message temp = (Message) obj;

return timeSent.Compar eTo(temp.TimeSe nt);
}
else
{
return 0;
}
}

if anyone can help me with this i'd be grateful. cheers!

Dec 9 '05 #2
Charlie Bear wrote:
i wonder if anyone can help...

i have an abstract class called "Message" that impliment's icompareable
on the property "TimeSent".
i have two types of messages that inherit the "Message" class - Timed
Messages and Questions.
i get some of both and put them into an array list and then use the
Sort method. I need it to sort them by TimeSent regardless of type but
it groups them into questions and timedmessages. i could have sworn i
got this working at one point or maybe i just wished i had.
i thought that since they both inherit "Message" and "Message"
imliements icompareable it would sort them all as messages. the code
i'm using in "Message" is:


<snip>

Unless you're overriding CompareTo in the derived classes, that should
be working fine.

Could you post a short but complete program that demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Dec 9 '05 #3
i built a "complete program" to send to you requested and discovered
that it works in that! i must be doing something dumb somewhere...

thanks for your help. i guess i just wanted to know if i was doing
something fundamentally wrong as i'm pretty new to OO.

Dec 9 '05 #4

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

Similar topics

3
1339
by: Ben Spigle | last post by:
I have a situtation where CompareTo is returning strange results when having dashes in strings. The most concise illustration is: "10050".CompareTo("1005-") = 1 "10050".CompareTo("1005-0") = -1 I have run alot more tests (I can post a short program if useful), but the rule seems to be - < 0 at the end of a string but - = 0 in the middle of a...
9
2063
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
4
9765
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base class are the private components internal to the base class itself. What I want are ALL components for the entire class no matter how many levels of...
5
5417
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the generic.List.IndexOf function doesn't appear to be invoking the contained class' CompareTo method. My understanding is that it should. The contained class...
2
9567
by: tony | last post by:
Hello! I have a class lets call it Test with four fields. This Test class implement IComparable so CompareTo is therefore implemented in this Test class. One field in this Test class is an int called Order. The other are of type double called EAF, C_min, C_aim and C_max I implement this CompareTo but I have run into some problems.
0
2813
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g.,...
4
2159
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any help on this, Benny
19
2213
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer...
3
10543
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you...
0
7488
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...
1
7520
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...
0
7845
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...
0
6101
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...
1
5403
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3532
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3517
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1978
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
1
1094
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.