473,586 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with overriding

Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Studen t source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you
Aug 23 '06 #1
5 1341
class Student : Person
{
public override void CopyFrom(Person person)
{
Student student = (Student) person;
// copy student properties
base.CopyFrom(s tudent);
}
}

?

"Thomas Lorenz" <th***********@ gmx.dewrote in message
news:10******** *************** ******@40tude.n et...
Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the
one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Studen t source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you

Aug 23 '06 #2
Thomas,

You can't do it the way you want. I believe this is called covariance.
You have to basically have your CopyFrom method take a Person parameter in
the derived class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Thomas Lorenz" <th***********@ gmx.dewrote in message
news:10******** *************** ******@40tude.n et...
Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the
one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Studen t source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you

Aug 23 '06 #3
I never recommend a hand-made copy constructor, because as time goes on,
properties change and they're can easily be left out of the copy
constructor.
I'd prefer to provide public access to MemberwiseClone .
"Eddy Young" <jeyoung@prisci mon_YOUR_CLOTHE S.comwrote in message
news:2006082414 540916807-jeyoung@priscim onYOURCLOTHESco m...
On 2006-08-23 20:48:35 +0100, Thomas Lorenz <th***********@ gmx.desaid:
Hi

I have a class Person and a class Student which inherits from Person.
Each
class should have a method which copies the properties from another
Person
or Student respectively. The copy-method of Student should override the
one
of Person and call it as well.
It should look like this (which won't compile):

class Person
{
public virtual void CopyFrom(Person source)
{
//Copy properties
}
}

class Student : Person
{
public override void CopyFrom(Studen t source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload
copy
method in Student which i don't want. There should only be one method in
each class with the according parameter.

How would I do that?

Thank you


Why not use a copy constructor?

Eddy
--
http://priscimon.com/blog

Remove your clothes before e-mailing me.

Aug 24 '06 #4
On 2006-08-23 15:48:35 -0400, Thomas Lorenz <th***********@ gmx.desaid:
Hi

I have a class Person and a class Student which inherits from Person. Each
class should have a method which copies the properties from another Person
or Student respectively. The copy-method of Student should override the one
of Person and call it as well.

You did not spell out why you want this; if you simply want to clone
Students, like this--

Student doyle = new Student();
// set properties of doyle here, then clone him
Student crabbe = doyle.Clone();
// change some properties of crabbe here

--then IClonaeable is the way to go. OTOH, If you want to clone a
Person into a Student, that's wrong because Person lacks the properties
of Student and thus cannot clone into a fully developed student. You
might try a hierarchy of assignment mehods for that purpose.

Anyway, back to cloning. O'Reilly's book, .NET Gotchas, recommends
something like this:

public class Person :
ICloneable
{
protected Person ( Person other ) {
// Copy Person attributes
}

public virtual object Clone() {
return new Person( this );
}
}
public class Student :
Person
{
protected Student( Student other ) :
base( other ) // <---- This copies Person attributes
{
// Copy Student attributes
}

public override object Clone() {
return new Student( this );
}
}
The key that makes it all work is the PROTECTED copy constructor in each class.

Aug 25 '06 #5
But surely you're not recommending?

Person(Person p)
{
this.name = p.Name;
this.title = p.Title;
// etc....
}

I'd go for reflection, or memberwise clone otherwise we're just introducing
a point of maintenance in the application aren't we?

"Eddy Young" <jeyoung@prisci mon_YOUR_CLOTHE S.comwrote in message
news:2006082514 052716807-jeyoung@priscim onYOURCLOTHESco m...
Yep. How I'd do it.

On 2006-08-25 13:18:01 +0100, Charles Jenkins
<no************ @not.here.comsa id:
On 2006-08-23 15:48:35 -0400, Thomas Lorenz <th***********@ gmx.desaid:
Hi

I have a class Person and a class Student which inherits from Person.
Each
class should have a method which copies the properties from another
Person
or Student respectively. The copy-method of Student should override the
one
of Person and call it as well.

You did not spell out why you want this; if you simply want to clone
Students, like this--

Student doyle = new Student();
// set properties of doyle here, then clone him
Student crabbe = doyle.Clone();
// change some properties of crabbe here

--then IClonaeable is the way to go. OTOH, If you want to clone a
Person into a Student, that's wrong because Person lacks the properties
of Student and thus cannot clone into a fully developed student. You
might try a hierarchy of assignment mehods for that purpose.

Anyway, back to cloning. O'Reilly's book, .NET Gotchas, recommends
something like this:

public class Person :
ICloneable
{
protected Person ( Person other ) {
// Copy Person attributes
}

public virtual object Clone() {
return new Person( this );
}
}
public class Student :
Person
{
protected Student( Student other ) :
base( other ) // <---- This copies Person attributes
{
// Copy Student attributes
}

public override object Clone() {
return new Student( this );
}
}
The key that makes it all work is the PROTECTED copy constructor in each
class.
>

--
http://priscimon.com/blog

Remove your clothes before e-mailing me.

Aug 25 '06 #6

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

Similar topics

3
3783
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for most operators, they are conspicuously absent for the logical "or" and "and". I'm guessing that the reason for this is that these operators...
3
4177
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return...
8
2253
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
19
2968
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and provide an open TD with a DIV in it for the content of this formlet. (The DIV is for DHTML to hide and show the content) I've created a web page...
2
3166
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the problems that I have encountered to date and the solutions (if any) that I found....
2
5425
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by...
3
1533
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
0
1865
by: David | last post by:
Hello all. I am trying to implement my first server control and have run into two problems that I cannot solve. I need the assistance of someone with more experience. My goal was to create an input button that would allow for both text and an image on it. I am attempting to accomplish this by basing off of the asp:button control. I...
10
105146
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they...
3
2194
by: =?ISO-8859-1?Q?Jo=E3o_Maia?= | last post by:
Hi there, I am having a weird problem in trying to use a ASP.NET menu inside a custom web part. I am developing a custom web part that has a menu inside (just the menu, nothing else). The menu is a System.Web.UI.WebControls.Menu (ASP.NET 2.0) object. If I add the menu to a simple ASPX web form, it works nice. However, I am adding it to my...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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
8338
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
7954
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
8215
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
6610
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...
1
5710
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...
0
5390
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
1179
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.