473,587 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Method handling a BaseClass parameter

I have a problem here. I've read a book about C# already and got the
basics of how the language handles polymorphism I think but I ran into a
problem here that I simply never even thought of as a problem coming
from C++:

I wrote some code similar to this:

abtract public class Base
{
}

public class DerivedA : Base
{
}

public class DerivedB : Base
{
}

public class MyClass
{
public void HandleSomething (DerivedA whatever)
{}
public void HandleSomething (DerivedB whatever)
{}
}
public class MyClassB
{
public void HandleSomething (Base whatever)
{
myThing.HandleS omething(whatev er);
}

private MyClass myThing;
}
Doing this I get the error that in MyClassB's method 'HandleSomethin g'
the parameter whatever cannot be converted to DerivedA or DerivedB
What's the logical reason for this error? The whole point of using a
common base class here was to enable me to handle all cases in one
method. Why does the compiler check this in this way? there will never
be a parameter that is just a "Base" because Base is abstract...

What's the correct way of doing this in C#?
Apr 20 '07 #1
5 1316
Andreas,

You have the example kind of turned around. It really should be this:

abtract public class Base
{
}

public class DerivedA : Base
{
}

public class DerivedB : Base
{
}

public class MyClass
{
public void HandleSomething (DerivedA whatever)
{
myClassB.Handle Something(whate ver);
}
public void HandleSomething (DerivedB whatever)
{
myClassB.Handle Something(whate ver);
}

private MyClassB myClassB;
}
public class MyClassB
{
public void HandleSomething (Base whatever)
{
// Do your logic here.
}
}

This example shows the advantages of polymorphism. With the one you
coded, you had a base class. The compiler can only know about classes that
the base class derives from (in other words, its parents) not about other
classes that derive from that class. For example, many classes can derive
from Base, and it is a one-to-many relationship. Because of that, the
compiler doesn't know which one to pass, hence the need to switch it around.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
Apr 20 '07 #2
Nicholas Paldino [.NET/C# MVP] schrieb:
You have the example kind of turned around. It really should be this:
My example is the way I want to use it and the way it would work in C++
and I have no idea why it does not work in C#.
This example shows the advantages of polymorphism. With the one you
coded, you had a base class. The compiler can only know about classes that
the base class derives from (in other words, its parents) not about other
classes that derive from that class.
Why must it know? I tell the compiler what function I want not to check
if everything could possibly work under all conditions. I don't care if
the compiler knows all derived classes. I do.
Hope this helps.
Sorry but it does not.
I still don't get why C# must be so strict about this. If I cannot use a
Base Class pointer (and those strange C# referenece types are supposed
to be exactly that according to every C# information I read so far) to
transport any object that is an instance of a derived class of the base
then what are Base class pointers for anyway?
if I can't get this to work I'll have to overload the handle method for
every single derived class that exists although that method does nothing
but call another method of a different class in which the overloading
becomes necessary. For that one method I'd overload 20 methods which all
do exactly the same. What's the sense of that?
I begin to really dislike C# and its compiler always telling me what
value can't be converted to whatother and what conversion might not be
safe and whatever. Am I the programmer or the compiler?
(Ignore the outburst, just a frustrated C++ lover talking)

Any further help would be appreciated
Apr 20 '07 #3
Andreas Schmitt schrieb:
Sorry but it does not.
I still don't get why C# must be so strict about this. If I cannot use a
Base Class pointer (and those strange C# referenece types are supposed
to be exactly that according to every C# information I read so far) to
transport any object that is an instance of a derived class of the base
then what are Base class pointers for anyway?
if I can't get this to work I'll have to overload the handle method for
every single derived class that exists although that method does nothing
but call another method of a different class in which the overloading
becomes necessary. For that one method I'd overload 20 methods which all
do exactly the same. What's the sense of that?
I begin to really dislike C# and its compiler always telling me what
value can't be converted to whatother and what conversion might not be
safe and whatever. Am I the programmer or the compiler?
(Ignore the outburst, just a frustrated C++ lover talking)

Any further help would be appreciated
I take everything back, I'm an idiot. This doesn't work in C++ either of
course.
Apr 20 '07 #4
Andreas Schmitt <ke**********@g mx.dewrote:
Nicholas Paldino [.NET/C# MVP] schrieb:
You have the example kind of turned around. It really should be this:

My example is the way I want to use it and the way it would work in C++
and I have no idea why it does not work in C#.
I'd be very surprised if it really *did* work in C++. Could you provide
an example which compiles in C++?
This example shows the advantages of polymorphism. With the one you
coded, you had a base class. The compiler can only know about classes that
the base class derives from (in other words, its parents) not about other
classes that derive from that class.

Why must it know? I tell the compiler what function I want not to check
if everything could possibly work under all conditions. I don't care if
the compiler knows all derived classes. I do.
Overloading is performed at compile-time, not execution time. The
compiler needs to know which of the methods to call, and it can't find
any that will definitely work.

The normal way round this is to use double-dispatch. See
http://en.wikipedia.org/wiki/Double_...tch_in_C.2B.2B
for some info on this.
Hope this helps.

Sorry but it does not.
I still don't get why C# must be so strict about this. If I cannot use a
Base Class pointer (and those strange C# referenece types are supposed
to be exactly that according to every C# information I read so far) to
transport any object that is an instance of a derived class of the base
then what are Base class pointers for anyway?
You can - you just can't change the fact that overloading is performed
at compile-time.
if I can't get this to work I'll have to overload the handle method for
every single derived class that exists although that method does nothing
but call another method of a different class in which the overloading
becomes necessary. For that one method I'd overload 20 methods which all
do exactly the same. What's the sense of that?
Use polymorphism instead. Put an empty virtual method in the base
class, and override it in each method you actually want to do something
in. Then just call whatever.SomeMe thod(myThing).
I begin to really dislike C# and its compiler always telling me what
value can't be converted to whatother and what conversion might not be
safe and whatever. Am I the programmer or the compiler?
(Ignore the outburst, just a frustrated C++ lover talking)
I think you're complaining about something you couldn't do in C++
either though.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 21 '07 #5
Not a problem. We've all had those "doh" moments. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andreas Schmitt" <ke**********@g mx.dewrote in message
news:f0******** *****@news.t-online.com...
Andreas Schmitt schrieb:
>Sorry but it does not.
I still don't get why C# must be so strict about this. If I cannot use a
Base Class pointer (and those strange C# referenece types are supposed to
be exactly that according to every C# information I read so far) to
transport any object that is an instance of a derived class of the base
then what are Base class pointers for anyway?
if I can't get this to work I'll have to overload the handle method for
every single derived class that exists although that method does nothing
but call another method of a different class in which the overloading
becomes necessary. For that one method I'd overload 20 methods which all
do exactly the same. What's the sense of that?
I begin to really dislike C# and its compiler always telling me what
value can't be converted to whatother and what conversion might not be
safe and whatever. Am I the programmer or the compiler?
(Ignore the outburst, just a frustrated C++ lover talking)

Any further help would be appreciated

I take everything back, I'm an idiot. This doesn't work in C++ either of
course.

Apr 22 '07 #6

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

Similar topics

1
2413
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using either the list.extend method or even by having the subclass create a whole new list in the variable. The following example illustrates the...
1
20690
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function init() {
10
1529
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the base class method is not to be abstract, and will be called if the instance was created as a 'generic' base class instance. It's sort of like I...
1
5145
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and ObjectDataSource binding to a SQLX db using a foreign key to filter the returned result set to display in a GridView. The error message in the...
6
1495
by: wingphil | last post by:
Hi there, I have a shared method in a base class, and I need to know which subclass it has been called from. So for example Public Mustinherit Class BaseClass Public Shared Sub SharedMethod ... End Sub
3
4425
by: skydivergm | last post by:
Hi, Consider the following code: --- template <class T> class TMyCWndWrapper : public T { public:
3
1542
by: Jonas Huckestein | last post by:
Hello, I have the following problem: I have one BaseClass, which defines the virtual functions getValue(int index) and virtually overloads the operators *, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the BaseClass and implement the virtual functions, so that I can go like this: BaseClass *oskar = new SubClass1;...
4
2006
by: amidzic.branko | last post by:
I'm trying to solve a problem using inheritance and polymorphism in python 2.4.2 I think it's easier to explain the problem using simple example: class shortList:
13
2278
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My problem is that classes in several modules share a common base class which needs to implement a factory method to return instances of these same...
0
7849
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...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
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...
1
7973
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
8220
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
5394
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.