Re: Type Casting
Carl,
No! it doesn't work, as soon as you attempt to run either GetClassA or
GetClassB, you get InvalidCastExce ption!
The other posts in this thread attempt to explain why your code fails.
Hope this helps
Jay
"Carl Tribble" <carltribble@sb cglobal.net> wrote in message
news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=blue]
> I just tried this and it works. Is this what you are looking for? -Carl.
>
> Option Explicit On
> Option Strict On
> Public Class A
> Inherits C
> End Class
> Public Class B
> Inherits C
> End Class
> Public Class C
> End Class
> Public Class Test
> Public Function GetClassA() As A
> Return CType(GetClassC (), A)
> End Function
> Public Function GEtClassB() As B
> Return CType(GetClassC (), B)
> End Function
> Public Function GetClassC() As C
> Return New C
> End Function
> End Class
>
>
>
>
> "René Nordby" <rene@bluedot.d k> wrote in message
> news:%23gDRMild EHA.212@TK2MSFT NGP12.phx.gbl.. .[color=green]
> > OK, thanks everyone for your replies.
> >
> > I will try to explain what I'm trying to do, and hopefully some one[/color][/color]
knows[color=blue][color=green]
> > how to do it.
> >
> > I want a Function to return a Class. With that Class in hand, I should[/color][/color]
be[color=blue][color=green]
> > able to Cast/Convert it, to two other Classes.
> >
> > In pseudo code it would look like this
> >
> > Sub GetClassA
> > myClassA = CType(GetClass, ClassA)
> > End Sub
> >
> > Sub GetClassB
> > myClassB = CType(GetClass, ClassB)
> > End Sub
> >
> > Function GetClass As ClassC
> > Return ClassC
> > End Function
> >
> > Is that possible, and how?
> >
> > Keep up the good work
> > René
> >
> > "René Nordby" <rene@bluedot.d k> wrote in message
> > news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
> > > 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[/color]
> > means[color=darkred]
> > > 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[/color]
> > object[color=darkred]
> > > become a B type.
> > >
> > >
> > >
> > > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
> > Specified[color=darkred]
> > > cast is not valid.', so want am I missing.
> > >
> > >
> > >
> > > Please give a hint.
> > >
> > >
> > >
> > > René
> > >
> > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
Carl,
No! it doesn't work, as soon as you attempt to run either GetClassA or
GetClassB, you get InvalidCastExce ption!
The other posts in this thread attempt to explain why your code fails.
Hope this helps
Jay
"Carl Tribble" <carltribble@sb cglobal.net> wrote in message
news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=blue]
> I just tried this and it works. Is this what you are looking for? -Carl.
>
> Option Explicit On
> Option Strict On
> Public Class A
> Inherits C
> End Class
> Public Class B
> Inherits C
> End Class
> Public Class C
> End Class
> Public Class Test
> Public Function GetClassA() As A
> Return CType(GetClassC (), A)
> End Function
> Public Function GEtClassB() As B
> Return CType(GetClassC (), B)
> End Function
> Public Function GetClassC() As C
> Return New C
> End Function
> End Class
>
>
>
>
> "René Nordby" <rene@bluedot.d k> wrote in message
> news:%23gDRMild EHA.212@TK2MSFT NGP12.phx.gbl.. .[color=green]
> > OK, thanks everyone for your replies.
> >
> > I will try to explain what I'm trying to do, and hopefully some one[/color][/color]
knows[color=blue][color=green]
> > how to do it.
> >
> > I want a Function to return a Class. With that Class in hand, I should[/color][/color]
be[color=blue][color=green]
> > able to Cast/Convert it, to two other Classes.
> >
> > In pseudo code it would look like this
> >
> > Sub GetClassA
> > myClassA = CType(GetClass, ClassA)
> > End Sub
> >
> > Sub GetClassB
> > myClassB = CType(GetClass, ClassB)
> > End Sub
> >
> > Function GetClass As ClassC
> > Return ClassC
> > End Function
> >
> > Is that possible, and how?
> >
> > Keep up the good work
> > René
> >
> > "René Nordby" <rene@bluedot.d k> wrote in message
> > news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
> > > 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[/color]
> > means[color=darkred]
> > > 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[/color]
> > object[color=darkred]
> > > become a B type.
> > >
> > >
> > >
> > > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
> > Specified[color=darkred]
> > > cast is not valid.', so want am I missing.
> > >
> > >
> > >
> > > Please give a hint.
> > >
> > >
> > >
> > > René
> > >
> > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
Comment