PC Review


Reply
Thread Tools Rate Thread

Type Casting

 
 
René Nordby
Guest
Posts: n/a
 
      29th Jul 2004
Hi there,



Is there anyone that knows how to do the following?



I have a class A and a class B, that 100% inherits from class A (this means
that I don't have other code in class B, than the Inherit statement).



Now I want to make a Type Casting on an object of the type A, so that object
become a B type.



I have tryied it, but gets the error 'System.InvalidCastException: Specified
cast is not valid.', so want am I missing.



Please give a hint.



René




 
Reply With Quote
 
 
 
 
Jeff Johnson [MVP: VB]
Guest
Posts: n/a
 
      29th Jul 2004

"René Nordby" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> I have a class A and a class B, that 100% inherits from class A (this

means
> that I don't have other code in class B, than the Inherit statement).
>
>
>
> Now I want to make a Type Casting on an object of the type A, so that

object
> become a B type.
>
>
>
> I have tryied it, but gets the error 'System.InvalidCastException:

Specified
> cast is not valid.', so want am I missing.


You're missing the fact that it can't be done. You can cast a derived class
into its base class, but you can't cast a base class to a derived class.
Someone correct me if I'm wrong.


 
Reply With Quote
 
 
 
 
René Nordby
Guest
Posts: n/a
 
      29th Jul 2004
Hi Jeff,

Thanks for your quick answer.

When you say so, then I thnink of String and Integer datatypes, where you
can cast from String to Integer and the other way around, so how is that
done?

I know that both String and Integer is derived from Object, so I have also
tryied to let my two classes inherit from a base class, but it give me the
same result.

"Jeff Johnson [MVP: VB]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "René Nordby" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
> > I have a class A and a class B, that 100% inherits from class A (this

> means
> > that I don't have other code in class B, than the Inherit statement).
> >
> >
> >
> > Now I want to make a Type Casting on an object of the type A, so that

> object
> > become a B type.
> >
> >
> >
> > I have tryied it, but gets the error 'System.InvalidCastException:

> Specified
> > cast is not valid.', so want am I missing.

>
> You're missing the fact that it can't be done. You can cast a derived

class
> into its base class, but you can't cast a base class to a derived class.
> Someone correct me if I'm wrong.
>
>



 
Reply With Quote
 
Nice Chap
Guest
Posts: n/a
 
      29th Jul 2004
Hi Jeff,

> You're missing the fact that it can't be done. You can cast a derived

class
> into its base class, but you can't cast a base class to a derived class.
> Someone correct me if I'm wrong.


You can indeed cast a base class reference to a sub-class reference and the
operation would result in a 'narrowing' conversion. BUT the conversion will
succeed ONLY if the base class reference you are trying to cast was
originally obtained from an earlier widening conversion from sub-class to
base-class.


 
Reply With Quote
 
René Nordby
Guest
Posts: n/a
 
      29th Jul 2004
Could you please spell it out a little more?

René

"Nice Chap" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Jeff,
>
> > You're missing the fact that it can't be done. You can cast a derived

> class
> > into its base class, but you can't cast a base class to a derived class.
> > Someone correct me if I'm wrong.

>
> You can indeed cast a base class reference to a sub-class reference and

the
> operation would result in a 'narrowing' conversion. BUT the conversion

will
> succeed ONLY if the base class reference you are trying to cast was
> originally obtained from an earlier widening conversion from sub-class to
> base-class.
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      29th Jul 2004
René

Have a look at this page.

http://msdn.microsoft.com/library/de...directcast.asp

Maybe it makes some things clear.

Cor


 
Reply With Quote
 
Anon-E-Moose
Guest
Posts: n/a
 
      29th Jul 2004
"Nice Chap" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> You can indeed cast a base class reference to a sub-class reference
> and the operation would result in a 'narrowing' conversion. BUT the
> conversion will succeed ONLY if the base class reference you are
> trying to cast was originally obtained from an earlier widening
> conversion from sub-class to base-class.


??? Can someone re-write this in clearer terms?

Thanks.

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      29th Jul 2004
In article <Xns9535AB438E36Danonemooseyahoocom@140.99.99.130>, Anon-E-Moose wrote:
> "Nice Chap" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> You can indeed cast a base class reference to a sub-class reference
>> and the operation would result in a 'narrowing' conversion. BUT the
>> conversion will succeed ONLY if the base class reference you are
>> trying to cast was originally obtained from an earlier widening
>> conversion from sub-class to base-class.

>
> ??? Can someone re-write this in clearer terms?
>
> Thanks.
>


What the op was saying was that, you can only cast a base class
reference to a subclass if that base class reference points to a
subclass instance.

In other words, casting a subclass to a base class is a narrowing
conversion, in that you loose access to some methods/properties of the
subclass since you'll be accessing it through the base class variable.
Casting a baseclass reference to a subclass would be a widening
conversion, since you would be picking up methods/properties. This is
not a safe operation, unless the base class reference actually points
to a subclass instance... Boy, this is confusing isn't it Here is
some psuedo code that may help...

class Base
end class

class SubClass
inherits base
end class

' this works
dim b as base = new subclass
dim s as subclass = ctype(b, subclass)

' and this works
dim s as subclass = new subclass
dim b as base = ctype(s, base)

' this doesn't
dim b as base = new base
dim s as subclass = ctype(b, subclass)


--
Tom Shelton [MVP]
 
Reply With Quote
 
Luhar
Guest
Posts: n/a
 
      29th Jul 2004
I believe the framework uses TypeConverters to cast between Strings &
Integer and vice versa. Google TypeConverter to get more info.

hth,

Luhar

"René Nordby" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Jeff,
>
> Thanks for your quick answer.
>
> When you say so, then I thnink of String and Integer datatypes, where you
> can cast from String to Integer and the other way around, so how is that
> done?
>
> I know that both String and Integer is derived from Object, so I have also
> tryied to let my two classes inherit from a base class, but it give me the
> same result.
>
> "Jeff Johnson [MVP: VB]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> >
> > "René Nordby" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> >
> > > I have a class A and a class B, that 100% inherits from class A (this

> > means
> > > that I don't have other code in class B, than the Inherit statement).
> > >
> > >
> > >
> > > Now I want to make a Type Casting on an object of the type A, so that

> > object
> > > become a B type.
> > >
> > >
> > >
> > > I have tryied it, but gets the error 'System.InvalidCastException:

> > Specified
> > > cast is not valid.', so want am I missing.

> >
> > You're missing the fact that it can't be done. You can cast a derived

> class
> > into its base class, but you can't cast a base class to a derived class.
> > Someone correct me if I'm wrong.
> >
> >

>
>



 
Reply With Quote
 
Carl Tribble
Guest
Posts: n/a
 
      30th Jul 2004
Rene, I think you should be able to do this. I think there is something
else going on. I have a project right now for example with a base class
LineItem and two classes deriving (inheriting) from it OrderItem and
ShipmentItem. I can use CTYPE() to perform any of the following
conversions:

OrderItem to LineItem
LineItem to OrderItem
ShipmentItem to LineItem
LineItem to ShipmentItem

The only thing I can not do is convert OrderItem to ShipmentItem, which
makes sense because they are not the same "thing". If you think about it,
OrderItem is a LineItem and ShipmentItem is a LineItem but OrderItem is not
a ShipmentItem.. You need to ask yourself what kind of real world objects
your Classes A and B represent and whether the conversion makes sense. Or
if you want to post you code we could look at it.

Good luck!
-Carl

"René Nordby" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi there,
>
>
>
> Is there anyone that knows how to do the following?
>
>
>
> I have a class A and a class B, that 100% inherits from class A (this

means
> that I don't have other code in class B, than the Inherit statement).
>
>
>
> Now I want to make a Type Casting on an object of the type A, so that

object
> become a B type.
>
>
>
> I have tryied it, but gets the error 'System.InvalidCastException:

Specified
> cast is not valid.', so want am I missing.
>
>
>
> Please give a hint.
>
>
>
> René
>
>
>
>



 
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
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
Casting to Date type =?Utf-8?B?RGF2aWQgQ2xlYXZl?= Microsoft Access Form Coding 2 18th Apr 2005 06:41 PM
Can't get value from DataSet without explicity type casting? Thomas H Microsoft ADO .NET 6 23rd Jul 2004 08:24 PM
Type casting object type to user defined class or interface Programmer Microsoft C# .NET 6 17th Jan 2004 09:15 AM
Type Casting at Runtime .Net CMaster383 Microsoft Dot NET Framework 2 26th Sep 2003 11:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:13 AM.