473,505 Members | 15,036 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(object obj)
{
if(obj is Message)
{
Message temp = (Message) obj;

return timeSent.CompareTo(temp.TimeSent);
}
else
{
return 0;
}
}

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

Dec 9 '05 #1
3 2177
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*****@contrapositive.tv> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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(object obj)
{
if(obj is Message)
{
Message temp = (Message) obj;

return timeSent.CompareTo(temp.TimeSent);
}
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
1331
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...
9
2057
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
9762
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...
5
5415
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...
2
9562
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...
0
2812
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...
4
2157
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...
19
2209
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...
3
10540
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...
0
7216
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
7303
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
7367
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
7471
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
5613
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,...
0
3187
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
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...

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.