By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ]

--
Dec 19 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a

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

Dec 19 '06 #2

P: n/a
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 ]

--
Dec 19 '06 #3

P: n/a
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 ]

--
Dec 19 '06 #4

P: n/a
ga**@garyshort.org 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 ]

--
Dec 19 '06 #5

P: n/a
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
Dec 19 '06 #6

P: n/a
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

Dec 19 '06 #7

P: n/a
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; }
}
}

Dec 19 '06 #8

P: n/a
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 ]
--
Dec 19 '06 #9

P: n/a
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 ]"" <kn****@NO-SPAM-PLEASE-gmail.comwrote
in message news:e3**************@TK2MSFTNGP04.phx.gbl...
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 ]
--

Dec 19 '06 #10

P: n/a
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.
--
Ignacio Machin
machin AT laceupsolutions com
Dec 19 '06 #11

P: n/a
Ignacio Machin ( .NET/ C# MVP ) escribió:
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.
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 ]

--

Dec 19 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.