User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Overloading question (C #) Page [1]  
EuroTitToss
All American
4790 Posts
user info
edit post

Ok. I feel really stupid here, but I'm trying to avoid this: "Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.

Three classes: A, B, C. B & C inherit A.

I have an "A" list, but it's filled with objects of type B & C. Basically, I want to do some kind of comparison on each object in this list to each other object. The comparison will be different based on the types involved, but I want to overload one comparison function instead of writing BcompareC, BcompareB, etc.

So I do two nested iterations through the list, and call my comparison method. But now the problem is they can't be converted from A to B or A to C. I'm not sure why... those objects were added as B or C to begin with. And I can't cast them myself without checking their type. This seems really basic. Halp?

2/15/2009 5:40:29 PM

A Tanzarian
drip drip boom
10995 Posts
user info
edit post

Virtual methods?

Interface class?

Or, put the comparison method in the A class. When comparing B and C it would seem that you'd only be comparing common properties...which should reside in a base class.

Disclaimer: I'm an eager novice with C#/OOP.

2/15/2009 8:01:19 PM

EuroTitToss
All American
4790 Posts
user info
edit post

That's my instinct... that I call the function and it figures out which function is overridden at runtime.

But in this case, I need it to figure out which function to call purely on the arguments. The way I'm thinking about it, it doesn't really matter where the comparison function resides.

[Edited on February 15, 2009 at 8:05 PM. Reason : .]

2/15/2009 8:04:42 PM

A Tanzarian
drip drip boom
10995 Posts
user info
edit post

Override a method.

I'm pretty sure it will differentiate between function(int, int) and function(long, int).

2/15/2009 8:13:01 PM

EuroTitToss
All American
4790 Posts
user info
edit post

ok, here's the problem.

I'm iterating through a list of As (which really are either Bs or Cs) with:
foreach(A a as v){
then doing another one nested within that.

I can't call: compare(b,c) without TWO casts and I can't cast without checking the type. All I can do is compare(a,a) and that doesn't really solve my problem.

maybe I need to generate the list differently or generate several lists. but I sure as shit don't want to have to copy code to iterate through each one.

[Edited on February 15, 2009 at 8:20 PM. Reason : .]

2/15/2009 8:18:11 PM

scud
All American
10804 Posts
user info
edit post

the general rule is that if you find yourself casting then you're doing it wrong
the concept you want is Polymorphism.

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://dotnetslackers.com/articles/csharp/PolymorphismEncapsulation.aspx
http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx

2/15/2009 8:27:01 PM

EuroTitToss
All American
4790 Posts
user info
edit post

I understand polymorphism and I don't think it's going to help me here as I've explained.

Yes, I get it. An animal can speak(), but a dog's speak() will be different from a pig's speak(). We can go through a list of Animals and hear them each speak...

I need polymorphism to extend to the arguments in the function though, not just the class being called on. My problem is more like a whole bunch of Animals getting in line for cage matches, one of which might be: cageMatch(Dog d, Pig p).

Maybe this is poor practice or impossible. I don't know.

[Edited on February 16, 2009 at 7:03 AM. Reason : .]

2/16/2009 7:01:45 AM

 Message Boards » Tech Talk » Overloading question (C #) Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.