Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems with overriding

Thomas Lorenz
Guest
 
Posts: n/a
#1: Aug 23 '06
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(Student 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

Simon Tamman
Guest
 
Posts: n/a
#2: Aug 23 '06

re: Problems with overriding


class Student : Person
{
public override void CopyFrom(Person person)
{
Student student = (Student) person;
// copy student properties
base.CopyFrom(student);
}
}

?

"Thomas Lorenz" <thomas.lorenz@gmx.dewrote in message
news:10tso8ygr6r2m$.ct33sevjcj53.dlg@40tude.net...
Quote:
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
Quote:
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(Student 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

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#3: Aug 23 '06

re: Problems with overriding


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]
- mvp@spam.guard.caspershouse.com

"Thomas Lorenz" <thomas.lorenz@gmx.dewrote in message
news:10tso8ygr6r2m$.ct33sevjcj53.dlg@40tude.net...
Quote:
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(Student 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

Simon Tamman
Guest
 
Posts: n/a
#4: Aug 24 '06

re: Problems with overriding


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@priscimon_YOUR_CLOTHES.comwrote in message
news:2006082414540916807-jeyoung@priscimonYOURCLOTHEScom...
Quote:
On 2006-08-23 20:48:35 +0100, Thomas Lorenz <thomas.lorenz@gmx.desaid:
>
Quote:
Hi

I have a class Person and a class Student which inherits from Person.
Each
Quote:
Quote:
class should have a method which copies the properties from another
Person
Quote:
Quote:
or Student respectively. The copy-method of Student should override the
one
Quote:
Quote:
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(Student source)
{
//Copy Student properties
}
}

If I don't use the virtual/override modifiers then I have an overload
copy
Quote:
Quote:
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.
>

Charles Jenkins
Guest
 
Posts: n/a
#5: Aug 25 '06

re: Problems with overriding


On 2006-08-23 15:48:35 -0400, Thomas Lorenz <thomas.lorenz@gmx.desaid:
Quote:
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.

Simon Tamman
Guest
 
Posts: n/a
#6: Aug 25 '06

re: Problems with overriding


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@priscimon_YOUR_CLOTHES.comwrote in message
news:2006082514052716807-jeyoung@priscimonYOURCLOTHEScom...
Quote:
Yep. How I'd do it.
>
On 2006-08-25 13:18:01 +0100, Charles Jenkins
<not.my.address@not.here.comsaid:
>
Quote:
On 2006-08-23 15:48:35 -0400, Thomas Lorenz <thomas.lorenz@gmx.desaid:
Quote:
Hi
>
I have a class Person and a class Student which inherits from Person.
Each
Quote:
Quote:
Quote:
class should have a method which copies the properties from another
Person
Quote:
Quote:
Quote:
or Student respectively. The copy-method of Student should override the
one
Quote:
Quote:
Quote:
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.
Quote:
>
>
--
http://priscimon.com/blog
>
Remove your clothes before e-mailing me.
>

Closed Thread