The assignment operator

  • Thread starter Thread starter mehdi_mousavi
  • Start date Start date
M

mehdi_mousavi

Hi,
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:

1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass

and consider the following code snippet:

MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;

Why the following assignment doesn't simply work?

c1 = (ThirdNamespace.MyBaseClass)c2;

How am I supposed to do the above mentioned assignment?

Any help would be highly appreciated,

TIA,
Mehdi
 
mehdi_mousavi said:
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:

1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass

and consider the following code snippet:

MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;

Why the following assignment doesn't simply work?

c1 = (ThirdNamespace.MyBaseClass)c2;

How am I supposed to do the above mentioned assignment?

You can't. Firstly, namespaces are irrelevant here. Secondly, let's
consider a "real-world" example - a base class of Animal, with Dog and
Cat derived classes. Your code is trying to do:

Dog dog;
Cat cat;
....
dog = (Animal) cat;

Can you see why that shouldn't work? What would you expect to happen if
you tried to ask "dog" to bark after that assignment?

Leaving aside the fact that it was a cat, you can't do:

myDerivedClassVariable = (MyBaseClass) something;

because not every instance of the base class is an instance of the
specific derived class. For example, you can't do:

MemoryStream stream = new MemoryStream();
string x = (object) stream;

Jon
 
hi,

mehdi_mousavi said:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass

and consider the following code snippet:

MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;

Why the following assignment doesn't simply work?

c1 = (ThirdNamespace.MyBaseClass)c2;

How am I supposed to do the above mentioned assignment?
Don't think that this will work in any other OO language.

Maybe this will work:

ThirdNamespace.MyBaseClass polymorph1 = null, polymorph2 = null;

polymorph1 = (ThirdNamespace.MyBaseClass)c1;
polymorph2 = polymorph1;

((MyNamespace1.FirstClass) polymorph2).DoSomething();


mfG
--> stefan <--
 
Hi,
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:

1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass

and consider the following code snippet:

MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;

Why the following assignment doesn't simply work?

c1 = (ThirdNamespace.MyBaseClass)c2;

How am I supposed to do the above mentioned assignment?

Any help would be highly appreciated,

TIA,
Mehdi

The fact that those classes are defined in separate namespaces doesn't
matter. It's the fact that both FirstClass and SecondClass are "more"
than a MyBaseClass (at least as far as the compiler is concerned).

The cast from c2 to MyBaseClass works, because you tell the compiler to
"ignore" the extras (=what SecondClass has more than MyBaseClass). But
the assignment of that MyBaseClass to c1 fails, because the compiler
can't "add" the other extra's (=what FirstClass has more than
MyBaseClass).

Maybe (that would depend on how those derived classes really differ)
you can write a cast-operator (or a conversion method) from SecondClass
to FirstClass (if you still want to be able to do this assignment).

Hans Kesting
 
Wow, I thought it works under C++. However, it also gives the C2679
error. Anyway, let's back to C#. How am I supposed to write a cast
operator?

TIA,
Mehdi
 
mehdi_mousavi said:
Wow, I thought it works under C++. However, it also gives the C2679
error. Anyway, let's back to C#. How am I supposed to write a cast
operator?

They're actually called 'conversion operators', and that's the name of
the help topic, or just follow links from the help for the 'implicit'
and 'explicit' keywords.
 
Back
Top