Casting issue

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hey guys

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that parent
bound to the interface


class myClassA : myOtherClass //a class that inherits


then in my code i do this:

myClassA obj = (myClassA)myOtherClass; //<----and this throws a cannot
cast myOtherCLass to myCLassA error


myOtherClass has no private variables. Why can't it cast?
 
i have a set up like this:
Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that
parent bound to the interface

class myClassA : myOtherClass //a class that inherits

then in my code i do this:

myClassA obj = (myClassA)myOtherClass; //<----and this throws a
cannot cast myOtherCLass to myCLassA error

myOtherClass has no private variables. Why can't it cast?

Is this actually the code your using? If so, you can't cast a class name,
you can only cast an instance of a class. So, this code is illegal:

myClass obj = (myClassA)myOtherClass;

But, this code should work:

myOtherClass instance = new myOtherClass();
myClass obj = (myClassA)instance;

Best Regards,
Dustin Campbell
Developer Express Inc
 
AHH! Sorry i made a mistake in my example. Yes that was meant to be an
instance.

Ok corrected:

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that parent
bound to the interface


class myClassA : myOtherClass //a class that inherits


then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and this
throws a cannot cast myOtherCLass to myCLassA error


myOtherClass has no private variables. Why can't it cast?

Since you said it should cast i presume you see my above coe as right. So
what else could it be? The only other thing i have is that the myClass is
serialisable as is the myClassA. Would that cause an issue/ i don't see why?
But then i don't get why this wont work either.
 
AHH! Sorry i made a mistake in my example. Yes that was meant to be an
instance.

Ok corrected:

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that
parent bound to the interface

class myClassA : myOtherClass //a class that inherits

then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and
this throws a cannot cast myOtherCLass to myCLassA error

myOtherClass has no private variables. Why can't it cast?

Actually, I hadn't thought my code through very well either. The variable
needs to be an instance of myClassA to cast. Like this:

myOtherClass instance = new myClassA();
myClassA obj = (myClassA)instance;

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Dustin said:
But, this code should work:

myOtherClass instance = new myOtherClass();
myClass obj = (myClassA)instance;

I almost missed it too, but that will throw an InvalidCastException
since myOtherClass is not a myClassA.

Brian
 
Yeah i spotted that. Basically my code should work. So I ask again? Why
doeesnt it?

2 objects being cast have same methods......how can they not cast? Anyway i
can find out details on why it can't cast....?
 
Daniel said:
AHH! Sorry i made a mistake in my example. Yes that was meant to be an
instance.

Ok corrected:

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that parent
bound to the interface


class myClassA : myOtherClass //a class that inherits


then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and
this
throws a cannot cast myOtherCLass to myCLassA error


myOtherClass has no private variables. Why can't it cast?

Since you said it should cast i presume you see my above coe as right. So
what else could it be? The only other thing i have is that the myClass is
serialisable as is the myClassA. Would that cause an issue/ i don't see
why? But then i don't get why this wont work either.

Hi,

It shouldn't be able to cast at all, because myClassA is not a sub-class of
myOtherClass. You have an instance to myOtherClass, which is *not* a
myClassA.

The cast is indeed, invalid.
 
Dustin Campbell said:
[...]
But, this code should work:

myOtherClass instance = new myOtherClass();
myClass obj = (myClassA)instance;

Why do you think that code should work?

If you had written "myClass obj = (myClass)instance;", I would have agreed
that it would work. But it also wouldn't be what the original poster asked
for.

As written though, you have the same problem with your code than he has with
his.

Pete
 
Daniel said:
[...]
then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and
this throws a cannot cast myOtherCLass to myCLassA error

myOtherClass has no private variables. Why can't it cast?

It can't cast because "some_Instance_of_myOtherClass" isn't an instance of
"myClassA".

The lack of private variables is entirely irrelevant. You cannot create a
more-derived class out of a less-derived one. It has to start as a
more-derived class in the first place.

This works:

myOtherClass moc = new myClassA(); // myClassA inherits from myOtherClass,
so an instance of myClassA *is* also an instance of myOtherClass
myClassA obj = (myClassA)moc;

This does not:

myOtherClass moc = new myOtherClass();
myClassA obj = (myClassA)moc; // moc was originally created only as an
instance of myOtherClass...you can't change it into an instance of myClassA
after the fact

It's not legal to cast an instance to a class that it's not.

If you still do not understand, see this thread:
http://groups.google.com/group/micr...read/thread/c38381ea1c38abbe/4a73d4c86adc6a41

I posted a contribution to that thread that I hope does a decent job of
explaining how casting and inheritence relate to each other:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/bf71467fd76fde12

Pete
 
Sorry i made such a mess of my description, i just reaalised i described it
as tho it was linear. i have reposted a MUCH clearer explanation....please
still help!

Sorry about that guys
 

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

Back
Top