473,729 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should this type of method overload work in C#?

public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(Subclass derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}

I expect this to work, but I tested it at runtime, verifying that the
instance is of the Derived class and it still calls the signature with the
base class.

thanks,

Dave Raskin
Nov 17 '05 #1
19 1941
Howdy. I believe I got it to work properly (as you expected) for me.
Here's the code I used (slightly modified to get it to compile). Output at
the end.
//--------------------------------------------------------------start code
using System;

public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.WriteLi ne("Called public void Send(Base baseObject) signature");
}

public void Send(Derived derivedObject)
{
// send derivedObject
Console.WriteLi ne("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}
//--------------------------------------------------------------end code
And here's the output I see.

C:\temp>basetes ting
Called public void Send(Base baseObject) signature
Called public void Send(Subclass derivedObject) signature

C:\temp>
Nov 17 '05 #2
Howdy. I believe I got it to work properly (as you expected) for me.
Here's the code I used (slightly modified to get it to compile). Output at
the end.
//--------------------------------------------------------------start code
using System;

public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.WriteLi ne("Called public void Send(Base baseObject) signature");
}

public void Send(Derived derivedObject)
{
// send derivedObject
Console.WriteLi ne("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}
//--------------------------------------------------------------end code
And here's the output I see.

C:\temp>basetes ting
Called public void Send(Base baseObject) signature
Called public void Send(Subclass derivedObject) signature

C:\temp>
Nov 17 '05 #3
Hi Dave,

Service doesn't have an overload with a Derived parameter, so the compiler
used a best match that took the overload with the Base parameter. From the
information you've given, I can't tell what SubClass is. However, if you
change the definition of Send(SubClass derivedObject) to this, you could get
what you expect:

public void Send(Derived derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Derived derivedObject)
signature");
}

Joe
--
http://www.csharp-station.com

"Dave Raskin" <Dave Ra****@discussi ons.microsoft.c om> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(Subclass derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}

I expect this to work, but I tested it at runtime, verifying that the
instance is of the Derived class and it still calls the signature with the
base class.

thanks,

Dave Raskin

Nov 17 '05 #4
Hi Dave,

Service doesn't have an overload with a Derived parameter, so the compiler
used a best match that took the overload with the Base parameter. From the
information you've given, I can't tell what SubClass is. However, if you
change the definition of Send(SubClass derivedObject) to this, you could get
what you expect:

public void Send(Derived derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Derived derivedObject)
signature");
}

Joe
--
http://www.csharp-station.com

"Dave Raskin" <Dave Ra****@discussi ons.microsoft.c om> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(Subclass derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}

I expect this to work, but I tested it at runtime, verifying that the
instance is of the Derived class and it still calls the signature with the
base class.

thanks,

Dave Raskin

Nov 17 '05 #5
You're right, the second Send() signautre should have Derived as param, not
Subclass.

Thanks for your answer. My case didn't work, although what I presented here
is a simplified situation, which I thought was essentially the same, but may
be I missed something. I will take a look.

"Flip" wrote:
Howdy. I believe I got it to work properly (as you expected) for me.
Here's the code I used (slightly modified to get it to compile). Output at
the end.
//--------------------------------------------------------------start code
using System;

public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.WriteLi ne("Called public void Send(Base baseObject) signature");
}

public void Send(Derived derivedObject)
{
// send derivedObject
Console.WriteLi ne("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}
//--------------------------------------------------------------end code
And here's the output I see.

C:\temp>basetes ting
Called public void Send(Base baseObject) signature
Called public void Send(Subclass derivedObject) signature

C:\temp>

Nov 17 '05 #6
You're right, the second Send() signautre should have Derived as param, not
Subclass.

Thanks for your answer. My case didn't work, although what I presented here
is a simplified situation, which I thought was essentially the same, but may
be I missed something. I will take a look.

"Flip" wrote:
Howdy. I believe I got it to work properly (as you expected) for me.
Here's the code I used (slightly modified to get it to compile). Output at
the end.
//--------------------------------------------------------------start code
using System;

public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.WriteLi ne("Called public void Send(Base baseObject) signature");
}

public void Send(Derived derivedObject)
{
// send derivedObject
Console.WriteLi ne("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}
//--------------------------------------------------------------end code
And here's the output I see.

C:\temp>basetes ting
Called public void Send(Base baseObject) signature
Called public void Send(Subclass derivedObject) signature

C:\temp>

Nov 17 '05 #7
The signature with Subclass should be with Derived, sorry.

"Joe Mayo" wrote:
Hi Dave,

Service doesn't have an overload with a Derived parameter, so the compiler
used a best match that took the overload with the Base parameter. From the
information you've given, I can't tell what SubClass is. However, if you
change the definition of Send(SubClass derivedObject) to this, you could get
what you expect:

public void Send(Derived derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Derived derivedObject)
signature");
}

Joe
--
http://www.csharp-station.com

"Dave Raskin" <Dave Ra****@discussi ons.microsoft.c om> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(Subclass derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}

I expect this to work, but I tested it at runtime, verifying that the
instance is of the Derived class and it still calls the signature with the
base class.

thanks,

Dave Raskin


Nov 17 '05 #8
The signature with Subclass should be with Derived, sorry.

"Joe Mayo" wrote:
Hi Dave,

Service doesn't have an overload with a Derived parameter, so the compiler
used a best match that took the overload with the Base parameter. From the
information you've given, I can't tell what SubClass is. However, if you
change the definition of Send(SubClass derivedObject) to this, you could get
what you expect:

public void Send(Derived derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Derived derivedObject)
signature");
}

Joe
--
http://www.csharp-station.com

"Dave Raskin" <Dave Ra****@discussi ons.microsoft.c om> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
public class Base
{
}

public class Derived : Base
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(Subclass derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(Subclass derivedObject)
signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
Base baseObject = new Base();
Derived derivedObject = new Derived();

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
service.Send(ba seObject);

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
service.Send(de rivedObject);
}
}

I expect this to work, but I tested it at runtime, verifying that the
instance is of the Derived class and it still calls the signature with the
base class.

thanks,

Dave Raskin


Nov 17 '05 #9
Ok, here's an example of where it fails. This has an added wrinkle of another
virtual method - Process().

public abstract class Base
{
protected Service service = new Service();

public abstract void Process();
}

public class BaseDerived : Base
{
public override void Process()
{
this.service.Se nd(this);
}
}

public class DerivedDerived : BaseDerived
{
}

public class Service
{
public void Send(Base baseObject)
{
// send baseObject
Console.Out.Wri teLine("Called public void Send(Base baseObject)
signature");
}

public void Send(DerivedDer ived derivedObject)
{
// send derivedObject
Console.Out.Wri teLine("Called public void Send(DerivedDer ived
derivedObject) signature");
}
}

public class MainClass
{
public static void Main(string[] args)
{
BaseDerived baseObject = new BaseDerived();
DerivedDerived derivedObject = new DerivedDerived( );

Service service = new Service();

// this calls public void Send(Base baseObject) as expected
baseObject.Proc ess();

// this calls public void Send(Base baseObject),
// but it should call public void Send(Subclass derivedObject),
// shouldn't it???
derivedObject.P rocess();
}
}
Nov 17 '05 #10

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

Similar topics

11
3561
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object but x.gettype returns 'string', so it knows it contains a string. If I want to create a variable "y" as the same type that variable x contains how would I do that without having to iterate thru every possible
1
3301
by: Joe HM | last post by:
Hello - I have two Enums for which I would like to define type conversions ... Public Enum eA A2 = 0 A2 = 1 End Enum Public Enum eB B1 = 2
4
3143
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my question In short my question is why when I pass a generic type directly to the formatObject function it works fine, but when I pass it to the checkText function where it is itself a generic argument, and then it is passed to formatObject, it is seen...
4
1940
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be performed and do just that. However, out of the two possible ways of doing this, neither seems to work. I thought I could either... 1) overload the method just based on the generics argument Public Function GetData (Of T As SqlDataReader)...
0
9426
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2
2680
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.