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 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>
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>
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
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
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>
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>
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
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
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();
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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...
|
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)...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |