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

  • Thread starter =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
  • Start date

?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

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 ]

--
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Andrés G. Aragoneses said:
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
 
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

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 ]

--
 
G

gary

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());
}
 
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

(e-mail address 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 ]

--
 
M

Marc Gravell

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
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Andrés G. Aragoneses said:
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
 
M

Marc Gravell

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; }
}
}
 
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

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 ]


--
 
D

Dave Sexton

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 ]" said:
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 ]


--
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
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).

I think you have a little confusion, it's true that the derived class
contain the parent properties. but what you are doing is a "cloning"
operation. You have one instance of Parent and you want to create another
instance (it does not matter if it's a Parent or Derived) you still need to
copy the values from the first instance to the new instance.
 
Ad

Advertisements

?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

Ignacio Machin ( .NET/ C# MVP ) escribió:
Hi,


I think you have a little confusion, it's true that the derived class
contain the parent properties. but what you are doing is a "cloning"
operation. You have one instance of Parent and you want to create another
instance (it does not matter if it's a Parent or Derived) you still need to
copy the values from the first instance to the new instance.

Mmmmm, I think you're right. I think I got confused thinking that I
could change the type of the object at runtime, so as to transform it
into the derived class (adding the new properties and avoiding the N
cloning operations). But now I see that this would be a new concept that
the runtime should support (not only the language), I think...

Regards,

Andrés [ knocte ]

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top