PC Review


Reply
Thread Tools Rate Thread

The assignment operator

 
 
mehdi_mousavi
Guest
Posts: n/a
 
      18th Jan 2007
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      18th Jan 2007
mehdi_mousavi wrote:
> 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

 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      18th Jan 2007
hi,

mehdi_mousavi wrote:
> 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 <--
 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      18th Jan 2007
> 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


 
Reply With Quote
 
mehdi_mousavi
Guest
Posts: n/a
 
      18th Jan 2007
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

Hans Kesting wrote:
> > 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


 
Reply With Quote
 
Larry Lard
Guest
Posts: n/a
 
      18th Jan 2007
mehdi_mousavi wrote:
> 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.

--
Larry Lard
(E-Mail Removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      18th Jan 2007
> 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



http://msdn2.microsoft.com/en-us/library/ms173105.aspx

should help you in the right direction.


Hans Kesting

>
> Hans Kesting wrote:
>>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment Operator Alternative Funky Microsoft C# .NET 2 16th Oct 2006 10:52 PM
assignment to overloaded operator << Ian Lazarus Microsoft C# .NET 5 19th Dec 2004 05:26 PM
Assignment Operator news.microsoft.com Microsoft Excel Discussion 3 23rd Apr 2004 07:02 AM
Assignment operator in Excel =?Utf-8?B?QW53YXI=?= Microsoft Excel Misc 1 31st Mar 2004 10:00 AM
Assignment operator can NOT be overloaded ! Christian Microsoft C# .NET 2 16th Mar 2004 02:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:06 PM.