473,511 Members | 14,846 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.HandleSomething(whatever);
}

private MyClass myThing;
}
Doing this I get the error that in MyClassB's method 'HandleSomething'
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 1310
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.HandleSomething(whatever);
}
public void HandleSomething(DerivedB whatever)
{
myClassB.HandleSomething(whatever);
}

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.com
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**********@gmx.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.SomeMethod(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.com>
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.com

"Andreas Schmitt" <ke**********@gmx.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
2409
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...
1
20674
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...
10
1514
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...
1
5137
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...
6
1489
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...
3
4415
by: skydivergm | last post by:
Hi, Consider the following code: --- template <class T> class TMyCWndWrapper : public T { public:
3
1534
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...
4
1990
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
2265
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...
0
7252
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
7153
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...
0
7371
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,...
1
7093
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...
0
7517
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...
1
5077
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
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 ...
1
791
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.