Object Casting Question

A

Andrea Williams

I have two objects and one inherits the other and adds a few more
properties. Say I populate the first object with the properties, then want
to cast it to be the second object, then I would be able to add the extra
propety value proerties and save the data based on the new object.

Example:
Object one has these properties:
FirstName
LastName

Object two inherits from Object one and adds these properties:
MiddleInitial
Title

So I create object one, fill it using a database query from a particular
table. then I want to be able to cast it to object two, add the extra
properties and save it to a different table in the database. Currently, I'm
just doing this:

object2.FirstName = object1.FirstName;
object2.LastName = object1.LastName;

But is there a way that I could implement an interface or something that
would let me cast the object1 to object2?

Any articles about this.

Thanks in advance,
Andrea Williams
 
N

Nicholas Paldino [.NET/C# MVP]

Andrea,

You should just create an instance of the second object, and set the
properties then. If you have an instance of the first object, you will need
to copy the values over to a new instance of the second object. In .NET you
can only cast down to a base class (because it is guaranteed that the base
class can be cast down to). You can't make any guarantees going up.

Hope this helps.
 
G

Guest

Nicholas Paldino said:
Andrea,

You should just create an instance of the second object, and set the
properties then. If you have an instance of the first object, you will need
to copy the values over to a new instance of the second object. In .NET you
can only cast down to a base class (because it is guaranteed that the base
class can be cast down to). You can't make any guarantees going up.

so which way is down again? I thought derived to base is up-casting.
 
N

Nicholas Paldino [.NET/C# MVP]

Daniel,

It depends on perspective. I guess from mine, I consider casting to the
base class to be down, and to a derived class to be up.
 
J

John Wood

It may make more sense to think about the instancing.

Perhaps rather than create object1, you should be creating object2 but
passing the instance to the routine that populates the object as an object1.

eg.
Object2 obj = new Object2();
GetAndPopulate(obj);

In this case, GetAndPopulate() takes an Object1 class, and so the casting is
implicit. You can then proceed to add extra properties after the call to
GetAndPopulate using the Object2 instance.

Hope that helps.
John
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
It depends on perspective. I guess from mine, I consider casting to the
base class to be down, and to a derived class to be up.

I don't think that's the commonly used way of looking at it, I'm
afraid.

Certainly

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vccelng/htm/express_72.asp

(the C/C++ reference) talks about upcasting being from derived type to
base, and downcasting being from base to derived type. I can't find
anything in the C# language spec, but I'm pretty sure this is the
normal terminology.
 
A

Anders Borum

Hello!

Exactly, just as one would picture it with a normal top-down hierarchical
graph. I tried to visualize it the other way around, but I got a "does not
compute" error internally ;-)
 
A

Andrea Williams

Thanks everyone. This is pretty much what I thought, but hoped someone
would know more about the various Interfaces available and if they may make
it a bit easier.

thanks,
Andrea
 

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