PC Review


Reply
Thread Tools Rate Thread

Cast from parent(base) to son(derived) class?

 
 
=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
Guest
Posts: n/a
 
      19th Dec 2006
I have a simple class:

public class BaseClass
{
private string propertyA;

protected string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}

private string propertyB;

protected string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

Now I have a derived class:

public class DerivedClass
{
private string propertyC;

protected string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
}

How can I create a constructor method of DerivedClass that receives an
object from the base class and casts it to derived class (so as to copy
the value of the properties they have in common)?

I ask this because this constructor obviously raises a compile error:

public DerivedClass(BaseClass oObj)
{
this = (DerivedClass)oObj;
}

Is there a method to do this apart from Reflection?

Thanks in advance.

Andrés [ knocte ]

--
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Dec 2006

Andrés G. Aragoneses [ knocte ] wrote:
> I have a simple class:
>
> public class BaseClass
> {
> private string propertyA;
>
> protected string PropertyA
> {
> get { return propertyA; }
> set { propertyA = value; }
> }
>
> private string propertyB;
>
> protected string PropertyB
> {
> get { return propertyB; }
> set { propertyB = value; }
> }
> }
>
> Now I have a derived class:
>
> public class DerivedClass
> {
> private string propertyC;
>
> protected string PropertyC
> {
> get { return propertyC; }
> set { propertyC = value; }
> }
> }
>
> How can I create a constructor method of DerivedClass that receives an
> object from the base class and casts it to derived class (so as to copy
> the value of the properties they have in common)?


If you only need to copy the properties they have in common, you don't
need to cast it.

Just use:

PropertyA = oObj.PropertyA;
PropertyB = oObj.PropertyB;

Jon

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
Guest
Posts: n/a
 
      19th Dec 2006
Jon Skeet [C# MVP] escribió:
> If you only need to copy the properties they have in common, you don't
> need to cast it.
>
> Just use:
>
> PropertyA = oObj.PropertyA;
> PropertyB = oObj.PropertyB;


Yes, but I want to avoid coding that, because it should be automatic (as
DerivedClass derives from BaseClass and thus it contains the same
properties).

Thanks.

Andrés [ knocte ]

--
 
Reply With Quote
 
gary@garyshort.org
Guest
Posts: n/a
 
      19th Dec 2006
Hello Andres,

The following code works for me...

class Parent
{
private int _A;

public int A
{
get { return _A; }
set { _A = value; }
}

private int _B;

public int B
{
get { return _B; }
set { _B = value; }
}


}

class Child : Parent
{
public Child(Parent p)
{
this.A = p.A;
this.B = p.B;
}

private int _C;

public int C
{
get { return _C; }
set { _C = value; }
}

}

private void button1_Click(object sender, EventArgs e)
{
Parent p = new Parent();
p.A = 1;
p.B = 2;

Child c = new Child(p);
c.C = 3;

MessageBox.Show(c.A.ToString());
}

--
Cheers,
Gary
http://www.garyshort.org/


Andrés G. Aragoneses [ knocte ] wrote:
> I have a simple class:
>
> public class BaseClass
> {
> private string propertyA;
>
> protected string PropertyA
> {
> get { return propertyA; }
> set { propertyA = value; }
> }
>
> private string propertyB;
>
> protected string PropertyB
> {
> get { return propertyB; }
> set { propertyB = value; }
> }
> }
>
> Now I have a derived class:
>
> public class DerivedClass
> {
> private string propertyC;
>
> protected string PropertyC
> {
> get { return propertyC; }
> set { propertyC = value; }
> }
> }
>
> How can I create a constructor method of DerivedClass that receives an
> object from the base class and casts it to derived class (so as to copy
> the value of the properties they have in common)?
>
> I ask this because this constructor obviously raises a compile error:
>
> public DerivedClass(BaseClass oObj)
> {
> this = (DerivedClass)oObj;
> }
>
> Is there a method to do this apart from Reflection?
>
> Thanks in advance.
>
> Andrés [ knocte ]
>
> --


 
Reply With Quote
 
=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
Guest
Posts: n/a
 
      19th Dec 2006
(E-Mail Removed) escribió:
> Hello Andres,
>
> The following code works for me...
>
> class Parent
> {
> private int _A;
>
> public int A
> {
> get { return _A; }
> set { _A = value; }
> }
>
> private int _B;
>
> public int B
> {
> get { return _B; }
> set { _B = value; }
> }
>
>
> }
>
> class Child : Parent
> {
> public Child(Parent p)
> {
> this.A = p.A;
> this.B = p.B;
> }
>
> private int _C;
>
> public int C
> {
> get { return _C; }
> set { _C = value; }
> }
>
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> Parent p = new Parent();
> p.A = 1;
> p.B = 2;
>
> Child c = new Child(p);
> c.C = 3;
>
> MessageBox.Show(c.A.ToString());
> }
>


Thanks, but what I want to do is avoid coding the copy of the
properties. I wondered if it was a way of doing that *without*
reflection. I guess there isn't any.

Regards,

Andrés [ knocte ]

--
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      19th Dec 2006
Well, extracting just the "base" part of a derived-class instance
isn't really what it is designed to do... if you need this behaviour,
berhaps a different design? Encapsulation - i.e. the "child" contains
the "parent", perhaps using an interface to expose the parent's
properties as a facade (so you can treat the "child" as an "IParent").
This allows you to simply obtain the encapsulated object directly
(assuming it is exposed as a property).

Not OO in inheritance terms, but then again inheritance isn't the
answer to every problem...

Marc


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Dec 2006
Andrés G. Aragoneses [ knocte ] wrote:
> Jon Skeet [C# MVP] escribió:
> > If you only need to copy the properties they have in common, you don't
> > need to cast it.
> >
> > Just use:
> >
> > PropertyA = oObj.PropertyA;
> > PropertyB = oObj.PropertyB;

>
> Yes, but I want to avoid coding that, because it should be automatic (as
> DerivedClass derives from BaseClass and thus it contains the same
> properties).


You'll need to code it *somewhere* unless you use reflection - you may
want to put it a base class constructor though.

Jon

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      19th Dec 2006
Example using your names... note that "baseclass" and "derivedclass"
are a little vaguewhen removed from inhetance, but I thought it would
be clearer to keep the same for reference purposes. Note also that the
base (either BaseClass or IBaseClass) could be made cloneable
(ICloneable) if you needed to make copies of just the base section.

Marc

using System;
class Program
{
static void Main()
{
DerivedClass dc = new DerivedClass();
dc.PropertyA = "A";
dc.PropertyB = "B";
dc.PropertyC = "C";
IBaseClass bc = dc.BaseClass;
}
}
public interface IBaseClass
{
string PropertyA { get;set;}
string PropertyB {get;set;}
}
public class BaseClass : IBaseClass
{
private string propertyA;
public string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}
private string propertyB;
public string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

public class DerivedClass : IBaseClass
{
public IBaseClass BaseClass { get { return baseClass; } }
private readonly IBaseClass baseClass;
public DerivedClass() : this(new BaseClass()) { }
public DerivedClass(IBaseClass baseClass)
{
if (baseClass == null) throw new ArgumentNullException();
this.baseClass = baseClass;
}
private string propertyC;
public string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
public string PropertyA
{
get { return baseClass.PropertyA; }
set { baseClass.PropertyA = value; }
}
public string PropertyB
{
get { return baseClass.PropertyB; }
set { baseClass.PropertyB = value; }
}
}



 
Reply With Quote
 
=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
Guest
Posts: n/a
 
      19th Dec 2006
Marc Gravell escribió:
> Example using your names... note that "baseclass" and "derivedclass"
> are a little vaguewhen removed from inhetance, but I thought it would
> be clearer to keep the same for reference purposes. Note also that the
> base (either BaseClass or IBaseClass) could be made cloneable
> (ICloneable) if you needed to make copies of just the base section.
>
> Marc
>
> using System;
> class Program
> {
> static void Main()
> {
> DerivedClass dc = new DerivedClass();
> dc.PropertyA = "A";
> dc.PropertyB = "B";
> dc.PropertyC = "C";
> IBaseClass bc = dc.BaseClass;
> }
> }
> public interface IBaseClass
> {
> string PropertyA { get;set;}
> string PropertyB {get;set;}
> }
> public class BaseClass : IBaseClass
> {
> private string propertyA;
> public string PropertyA
> {
> get { return propertyA; }
> set { propertyA = value; }
> }
> private string propertyB;
> public string PropertyB
> {
> get { return propertyB; }
> set { propertyB = value; }
> }
> }
>
> public class DerivedClass : IBaseClass
> {
> public IBaseClass BaseClass { get { return baseClass; } }
> private readonly IBaseClass baseClass;
> public DerivedClass() : this(new BaseClass()) { }
> public DerivedClass(IBaseClass baseClass)
> {
> if (baseClass == null) throw new ArgumentNullException();
> this.baseClass = baseClass;
> }
> private string propertyC;
> public string PropertyC
> {
> get { return propertyC; }
> set { propertyC = value; }
> }
> public string PropertyA
> {
> get { return baseClass.PropertyA; }
> set { baseClass.PropertyA = value; }
> }
> public string PropertyB
> {
> get { return baseClass.PropertyB; }
> set { baseClass.PropertyB = value; }
> }
> }




Thanks for the example, but I think it's not valid as an answer for the
initial question, because:

1) You're obtaining a BaseClass object from the DerivedClass, which is
the opposite what I wanted (I want to create a DerivedClass object from
the BaseClass object).

2) You have avoided the "cloning/copying code" but you have had to
implement the properties of the IBaseClass in DerivedClass, so then
you're writing more code that wouldn't have been necessary because those
properties were already in the BaseClass (all I want to do is avoid much
writing so as to be productive , I think I will end with a reflection
solution, or a new wishlist item for a future version of C# )

Thanks.

Andrés [ knocte ]


--
 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      19th Dec 2006
Hi Andrés,

> I think I will end with a reflection solution, or a new wishlist item for
> a future version of C# )


I say go with the wishlist (but don't count on it

--
Dave Sexton

""Andrés G. Aragoneses [ knocte ]"" <(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
> Marc Gravell escribió:
>> Example using your names... note that "baseclass" and "derivedclass"
>> are a little vaguewhen removed from inhetance, but I thought it would
>> be clearer to keep the same for reference purposes. Note also that the
>> base (either BaseClass or IBaseClass) could be made cloneable
>> (ICloneable) if you needed to make copies of just the base section.
>>
>> Marc
>>
>> using System;
>> class Program
>> {
>> static void Main()
>> {
>> DerivedClass dc = new DerivedClass();
>> dc.PropertyA = "A";
>> dc.PropertyB = "B";
>> dc.PropertyC = "C";
>> IBaseClass bc = dc.BaseClass;
>> }
>> }
>> public interface IBaseClass
>> {
>> string PropertyA { get;set;}
>> string PropertyB {get;set;}
>> }
>> public class BaseClass : IBaseClass
>> {
>> private string propertyA;
>> public string PropertyA
>> {
>> get { return propertyA; }
>> set { propertyA = value; }
>> }
>> private string propertyB;
>> public string PropertyB
>> {
>> get { return propertyB; }
>> set { propertyB = value; }
>> }
>> }
>>
>> public class DerivedClass : IBaseClass
>> {
>> public IBaseClass BaseClass { get { return baseClass; } }
>> private readonly IBaseClass baseClass;
>> public DerivedClass() : this(new BaseClass()) { }
>> public DerivedClass(IBaseClass baseClass)
>> {
>> if (baseClass == null) throw new ArgumentNullException();
>> this.baseClass = baseClass;
>> }
>> private string propertyC;
>> public string PropertyC
>> {
>> get { return propertyC; }
>> set { propertyC = value; }
>> }
>> public string PropertyA
>> {
>> get { return baseClass.PropertyA; }
>> set { baseClass.PropertyA = value; }
>> }
>> public string PropertyB
>> {
>> get { return baseClass.PropertyB; }
>> set { baseClass.PropertyB = value; }
>> }
>> }

>
>
>
> Thanks for the example, but I think it's not valid as an answer for the
> initial question, because:
>
> 1) You're obtaining a BaseClass object from the DerivedClass, which is the
> opposite what I wanted (I want to create a DerivedClass object from the
> BaseClass object).
>
> 2) You have avoided the "cloning/copying code" but you have had to
> implement the properties of the IBaseClass in DerivedClass, so then you're
> writing more code that wouldn't have been necessary because those
> properties were already in the BaseClass (all I want to do is avoid much
> writing so as to be productive , I think I will end with a reflection
> solution, or a new wishlist item for a future version of C# )
>
> Thanks.
>
> Andrés [ knocte ]
>
>
> --



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid cast from base to derived INeedADip Microsoft C# .NET 10 18th Sep 2007 03:03 PM
Issues Serializing A Derived Class and its Base using IXmlSerializable, Can't See Base Peter Nofelt Microsoft Dot NET 1 10th Feb 2006 09:30 PM
Re: Setting an Event Handler on data member of parent class in derived class Jon Skeet [C# MVP] Microsoft C# .NET 1 13th Jan 2006 07:30 AM
Cast from base class to a derived class =?Utf-8?B?Sm9l?= Microsoft ADO .NET 1 16th Mar 2005 05:49 PM
Cast from derived to base fails after Activator.CreateInstance() call Rob Richardson Microsoft C# .NET 3 29th Dec 2004 07:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:35 AM.