Down-Casting without Assigning ANY Variables?????

N

nautic20

Hey C# people,

I'm just trying to assign two (unlike) child objects which both derive
from the same base class equal to each other...

Example :

BaseClass tempObj = child1 as BaseClass;
child2 = (child2)tempObj;

Of course this throws an exception because child2 doesn't have all the
same properties as child1. I know I should map one to the other but
the BaseClass alone has 100+ properties and each child has about a
dozen themselves.

I KNOW there is a System method that can convert child1 to child2 by
simply copying the properties from one class and assigning them to the
same-name properties of the other; I've done it before. I JUST CAN'T
REMEMBER WHAT THE NAME OF THAT NAMESPACE.CLASS.METHOD WAS!!!. I'm
praying one of you guys knows because I'm not trying to get carpel-
tunnel syndrome!

If one of you all would enlighten me I PROMISE I'll instantly mail it
to my gmail account and NEVER forget it!!!!!!!!!!!!!!!

Thanks to all!
 
P

Peter Duniho

Hey C# people,

I'm just trying to assign two (unlike) child objects which both derive
from the same base class equal to each other...

Example :

BaseClass tempObj = child1 as BaseClass;
child2 = (child2)tempObj;

First issue is that the above is not a complete code example, so we don't
really know for sure what you're doing.

For the sake of discussion, I will assume something like this:

class BaseClass { ... }

class Derived1 : BaseClass { ... }

class Derived2 : BaseClass { ... }

Derived1 child1 = ...;
Derived2 child2;

BaseClass tempObj = child1; // no need to cast or use "as"

child2 = (Derived2)tempObj; // cast using the type name, not the
variable name

Then...
Of course this throws an exception because child2 doesn't have all the
same properties as child1.

No, that's not why the exception is thrown. The exception is thrown
because the code is trying to cast an instance of Derived1 to the type
Derived2, and Derived2 is not Derived1 or a base class of Derived1.

You could declare the type identically, with exactly the same properties,
and the exception would still be thrown.
I know I should map one to the other but
the BaseClass alone has 100+ properties and each child has about a
dozen themselves.

I KNOW there is a System method that can convert child1 to child2 by
simply copying the properties from one class and assigning them to the
same-name properties of the other; I've done it before.

I've never heard of such a method. I doubt it exists. It wouldn't be too
hard to write such a method yourself, using reflection. But AFAIK nothing
of the sort is already present in .NET.

Pete
 
J

Joe Greer

Hey C# people,

I'm just trying to assign two (unlike) child objects which both derive
from the same base class equal to each other...

The above statement makes it sound like you have some design issues in
general because you shouldn't be trying to assign things that are unlike to
each other. But, be that as it may, the way to do this kind of thing is to
factor the common bits into a class of its own and use containment in the
other classes. Then you just assign it. For example:


struct common // could be a class depending upon the semantics you want.
{
string name;
/// whatever
}

class A
{
public common c;
}

class B
{
public common c;
}


A a = new A();

B b = new B();

b.c = a.c;


HTH,
joe
 

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