Hi,
I have a puzzling case here:
using System;
namespace TestOverrides
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Ancestor dummy = new SubClass();
Container.Hello(dummy);
}
}
class Ancestor
{
public String Field1 = "Field1";
}
class SubClass : Ancestor
{
public int Field2 = 27;
}
class Container
{
public static void Hello(SubClass pParm)
{
Console.WriteLine(pParm.Field2);
}
public static void Hello(Ancestor pParm)
{
Console.WriteLine(pParm.Field1);
}
}
}
In this peice of code, I would have expected 27 to print, but it always
prints "Field1". It seems that it does not recognize that, although the
parameter is declared as «Ancestor», it really is of «SubClass» class. Any
suggestion here?
Thank you
Real 6 1276
But it's also an Ancestor, and that is the version it finds first.
What you really should have is a method called Hello in Ancestor, override
it in SubClass to do something different, and then call it like this:
static void Main(string[] args)
{
Ancestor dummy = new SubClass();
dummy.Hello();
}
"Réal Forté" <re********@dogico.com> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl... Hi,
I have a puzzling case here: using System;
namespace TestOverrides
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Ancestor dummy = new SubClass();
Container.Hello(dummy);
}
}
class Ancestor
{
public String Field1 = "Field1";
}
class SubClass : Ancestor
{
public int Field2 = 27;
}
class Container
{
public static void Hello(SubClass pParm)
{
Console.WriteLine(pParm.Field2);
}
public static void Hello(Ancestor pParm)
{
Console.WriteLine(pParm.Field1);
}
}
}
In this peice of code, I would have expected 27 to print, but it always prints "Field1". It seems that it does not recognize that, although the parameter is declared as «Ancestor», it really is of «SubClass» class. Any suggestion here?
Thank you
Real
It is not quite so simple. The various overrides have different signatures
and I do not want to force adding a new override in the ancestor every time
a new subclass is added (they reside in different dlls). I hear this type
of encapsulation is frequently used in Java where the compiler will
recognize the object for the subclass it is and select the appropriate
override. I would have expected C# to do the same.
Réal
"Marina" <so*****@nospam.com> wrote in message
news:Oz**************@TK2MSFTNGP12.phx.gbl... But it's also an Ancestor, and that is the version it finds first.
What you really should have is a method called Hello in Ancestor, override it in SubClass to do something different, and then call it like this:
static void Main(string[] args) {
Ancestor dummy = new SubClass(); dummy.Hello(); }
"Réal Forté" <re********@dogico.com> wrote in message news:OR**************@TK2MSFTNGP14.phx.gbl... Hi,
I have a puzzling case here: using System;
namespace TestOverrides
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Ancestor dummy = new SubClass();
Container.Hello(dummy);
}
}
class Ancestor
{
public String Field1 = "Field1";
}
class SubClass : Ancestor
{
public int Field2 = 27;
}
class Container
{
public static void Hello(SubClass pParm)
{
Console.WriteLine(pParm.Field2);
}
public static void Hello(Ancestor pParm)
{
Console.WriteLine(pParm.Field1);
}
}
}
In this peice of code, I would have expected 27 to print, but it always prints "Field1". It seems that it does not recognize that, although the parameter is declared as «Ancestor», it really is of «SubClass» class. Any suggestion here?
Thank you
Real
If a particular overload is only applicable in the descendent, it can be
added in only the descendent. It doesn't have to be in the ancestor.
"Réal Forté" <re********@dogico.com> wrote in message
news:OY**************@TK2MSFTNGP14.phx.gbl... It is not quite so simple. The various overrides have different signatures and I do not want to force adding a new override in the ancestor every time a new subclass is added (they reside in different dlls). I hear this type of encapsulation is frequently used in Java where the compiler will recognize the object for the subclass it is and select the appropriate override. I would have expected C# to do the same.
Réal
"Marina" <so*****@nospam.com> wrote in message news:Oz**************@TK2MSFTNGP12.phx.gbl... But it's also an Ancestor, and that is the version it finds first.
What you really should have is a method called Hello in Ancestor, override it in SubClass to do something different, and then call it like this:
static void Main(string[] args) {
Ancestor dummy = new SubClass(); dummy.Hello(); }
"Réal Forté" <re********@dogico.com> wrote in message news:OR**************@TK2MSFTNGP14.phx.gbl... Hi,
I have a puzzling case here: using System;
namespace TestOverrides
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Ancestor dummy = new SubClass();
Container.Hello(dummy);
}
}
class Ancestor
{
public String Field1 = "Field1";
}
class SubClass : Ancestor
{
public int Field2 = 27;
}
class Container
{
public static void Hello(SubClass pParm)
{
Console.WriteLine(pParm.Field2);
}
public static void Hello(Ancestor pParm)
{
Console.WriteLine(pParm.Field1);
}
}
}
In this peice of code, I would have expected 27 to print, but it always prints "Field1". It seems that it does not recognize that, although the parameter is declared as «Ancestor», it really is of «SubClass» class. Any suggestion here?
Thank you
Real
> In this peice of code, I would have expected 27 to print, but it always prints "Field1". It seems that it does not recognize that, although the parameter is declared as «Ancestor», it really is of «SubClass» class. Any suggestion here?
I'm pretty sure the decision for which Hello method should be used is
decided at compile time.
At compile time, all it knows about that variable is that it's declared type
is Ancestor. The assignment to it of "new SubClass" takes place at
runtime - the compiler has no way of predicting this.
Therefore, when the compiler sees a variable of Ancestor, it gives it to the
Hello method which takes a Ancestor as it's parameter.
Marina is right - this "Hello" method should be a virtual member of
ancestor, which SubClass can then change if it needs to. If a subclass of
ancestor does not need to change it, it can just leave it. This way you do
not have to keep adding methods to the Container class for each subclass of
Ancestor.
Ex:
public class Ancestor
{
public string Field1 = "Field1";
public virtual void DoHello()
{
Console.WriteLine(Field1);
}
}
public class SubClass : Ancestor
{
public int Field2 = 27;
public override void DoHello()
{
Console.WriteLine(Field2);
}
}
public class Container
{
public static void Hello(Ancestor pParm)
{
pParm.DoHello();
}
}
--
Adam Clauss
Thank you
Real
Réal Forté <re********@dogico.com> wrote: I have a puzzling case here:
<snip>
It's quite simple - you're not overriding, you're overloading.
Overloads are determined at compile-time, based on what the compile-
time types of the parameters are, not at run-time based on what the
actual values of the parameters are.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Réal Forté <re********@dogico.com> wrote: I have a puzzling case here:
<snip>
It's quite simple - you're not overriding, you're overloading.
Overloads are determined at compile-time, based on what the compile-
time types of the parameters are, not at run-time based on what the
actual values of the parameters are.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel Bolege |
last post by:
Hello,
I've a problem that drives me crazy. There must be a simple solution, but I
just can't see it. The following code is just a simple example that
demonstrates my problem:
I have an...
|
by: Shaun Wilde |
last post by:
I have a situation where I would like to choose the web.confg I get my
settings from.
I have 2 folders folderA and folderB and 3 web.configs the main root
web.config and a web.config in each of...
|
by: Bob Loveshade |
last post by:
I am looking for an example that shows how to select and highlight
multiple rows in a DataGrid.
My DataGrid is part of a Web User Control which is contained in an ASPX
page.
I haven't been...
|
by: miller504 |
last post by:
I'm trying to select several items from a listbox. My code is working
except when the form loads the first time. I have the code to select the
items inside sub Initialize_CbLeague. If I call sub...
|
by: koti |
last post by:
hi
i have written some code for selecting an item in combobox dropdown
list which is in datagrid columm. by scrolling the mouse we select any
item from the list.
but by pressing the down key...
|
by: Tina |
last post by:
I'm a VB.Net developer learning C# so this is another newbie question....
(using 1.1)
I posted a question about how to paste definitions for events in C# because
in VB we use the vs.net left...
|
by: Matt B |
last post by:
I know this is a bit of nonstandard behavior, but I would like the
Enter key to move focus through a group of radiobuttons without
selecting them. The user would then have to use Space or actually...
|
by: dan655t |
last post by:
Hello,
I've come here to get some advise from the experts. Currently I am
tasked with modifying a web service that currently supports one
product to support multiple products. It needs to be...
|
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= |
last post by:
Hi,
I have a moderately complex class (about 10 different string and int
type fields along with two different List<stringfields). I want to override
Equals(), so I need to override GetHashCode()....
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |