Type Casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay B. Harlow [MVP - Outlook]

    #16
    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]


    Comment

    • Cor Ligthert

      #17
      Re: Type Casting

      > I will try to explain what I'm trying to do, and hopefully some one knows[color=blue]
      > how to do it.
      >
      > I want a Function to return a Class. With that Class in hand, I should be
      > able to Cast/Convert it, to two other Classes.
      >[/color]


      You can not return a class, you can return an object.

      You convert no class, you cast or convert an object.

      Cor


      Comment

      • René Nordby

        #18
        Re: Type Casting

        Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work
        when it comes to runtime, so what should be done, to get it to work.

        I see what Jay means by returning the right object from start, but that's
        not the solution, sorry.

        The above code is what I want, but how should it be implemented, so it
        works?

        René

        "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

        • René Nordby

          #19
          Re: Type Casting

          Hi All,

          Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
          if anyone have any comments you are mostly welcome.

          Option Explicit On
          Option Strict On

          Public Class A
          Inherits B
          End Class

          Public Class B
          End Class

          Public Class C
          Inherits A
          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

          Private Function GetClassC() As C
          Return New C
          End Function
          End Class

          "René Nordby" <rene@bluedot.d k> wrote in message
          news:%23WRqudnd EHA.3944@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Absolutely, this is what I'm looking for, but as Jay writes, it doesn't[/color]
          work[color=blue]
          > when it comes to runtime, so what should be done, to get it to work.
          >
          > I see what Jay means by returning the right object from start, but that's
          > not the solution, sorry.
          >
          > The above code is what I want, but how should it be implemented, so it
          > works?
          >
          > René
          >
          > "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
          > news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=green]
          > > I just tried this and it works. Is this what you are looking[/color][/color]
          or? -Carl.[color=blue][color=green]
          > >
          > > 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=darkred]
          > > > 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=green][color=darkred]
          > > > how to do it.
          > > >
          > > > I want a Function to return a Class. With that Class in hand, I should[/color][/color]
          > be[color=green][color=darkred]
          > > > 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. ..
          > > > > 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[/color][/color][/color]
          (this[color=blue][color=green][color=darkred]
          > > > means
          > > > > that I don't have other code in class B, than the Inherit[/color][/color][/color]
          statement).[color=blue][color=green][color=darkred]
          > > > >
          > > > >
          > > > >
          > > > > Now I want to make a Type Casting on an object of the type A, so[/color][/color][/color]
          that[color=blue][color=green][color=darkred]
          > > > object
          > > > > become a B type.
          > > > >
          > > > >
          > > > >
          > > > > I have tryied it, but gets the error 'System.Invalid CastException:
          > > > Specified
          > > > > cast is not valid.', so want am I missing.
          > > > >
          > > > >
          > > > >
          > > > > Please give a hint.
          > > > >
          > > > >
          > > > >
          > > > > René
          > > > >
          > > > >
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • René Nordby

            #20
            Re: Type Casting

            Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work
            when it comes to runtime, so what should be done, to get it to work.

            I see what Jay means by returning the right object from start, but that's
            not the solution, sorry.

            The above code is what I want, but how should it be implemented, so it
            works?

            René

            "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

            • René Nordby

              #21
              Re: Type Casting

              Hi All,

              Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
              if anyone have any comments you are mostly welcome.

              Option Explicit On
              Option Strict On

              Public Class A
              Inherits B
              End Class

              Public Class B
              End Class

              Public Class C
              Inherits A
              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

              Private Function GetClassC() As C
              Return New C
              End Function
              End Class

              "René Nordby" <rene@bluedot.d k> wrote in message
              news:%23WRqudnd EHA.3944@tk2msf tngp13.phx.gbl. ..[color=blue]
              > Absolutely, this is what I'm looking for, but as Jay writes, it doesn't[/color]
              work[color=blue]
              > when it comes to runtime, so what should be done, to get it to work.
              >
              > I see what Jay means by returning the right object from start, but that's
              > not the solution, sorry.
              >
              > The above code is what I want, but how should it be implemented, so it
              > works?
              >
              > René
              >
              > "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
              > news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=green]
              > > I just tried this and it works. Is this what you are looking[/color][/color]
              or? -Carl.[color=blue][color=green]
              > >
              > > 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=darkred]
              > > > 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=green][color=darkred]
              > > > how to do it.
              > > >
              > > > I want a Function to return a Class. With that Class in hand, I should[/color][/color]
              > be[color=green][color=darkred]
              > > > 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. ..
              > > > > 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[/color][/color][/color]
              (this[color=blue][color=green][color=darkred]
              > > > means
              > > > > that I don't have other code in class B, than the Inherit[/color][/color][/color]
              statement).[color=blue][color=green][color=darkred]
              > > > >
              > > > >
              > > > >
              > > > > Now I want to make a Type Casting on an object of the type A, so[/color][/color][/color]
              that[color=blue][color=green][color=darkred]
              > > > object
              > > > > become a B type.
              > > > >
              > > > >
              > > > >
              > > > > I have tryied it, but gets the error 'System.Invalid CastException:
              > > > Specified
              > > > > cast is not valid.', so want am I missing.
              > > > >
              > > > >
              > > > >
              > > > > Please give a hint.
              > > > >
              > > > >
              > > > >
              > > > > René
              > > > >
              > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #22
                Re: Type Casting

                René,[color=blue]
                > Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
                > if anyone have any comments you are mostly welcome.[/color]
                No you didn't, as your GetClassB method will fail with a
                InvalidCastExce ption!
                [color=blue][color=green]
                > > The above code is what I want, but how should it be implemented, so it
                > > works?[/color][/color]
                It "cannot" be implemented in VS.NET 2003, as CType cannot change the type
                of a C object into a B or A object. It can however Cast a B or A object in a
                C variable to a B or A variable. Only predefined CTYPE conversions are
                defined in VS.NET 2003, C to A & C to B are not predefined. Also see my
                message to Nice chap about the difference between CType the conversion &
                DirectCast the casting.

                VS.NET 2005 (aka Whidbey, due out in 2005) you can overload CType such that
                it can create a B or A object given a C object, HOWEVER!!! your sample seems
                to be an extreme abuse of this ability! If you provide better details on
                what you are really trying to accomplish we may be able to offer other
                solutions then the two I offered earlier.

                The correct way (only way really) to implement this is:

                I would have GetClassA & GetClassB create the correct type of object, and
                not include GetClass.

                Alternatively you would need to pass a parameter to GetClass that indicates
                what kind of object to actually create. I would either use an Enum or the
                Type & Activator.Creat eInstance.
                [color=blue]
                > Public Function GetClassA() As A
                > Return DirectCast(GetC lassC(gettype(A )), A)
                > End Function
                >
                > Public Function GetClassB() As B
                > Return DirectCast(GetC lassC(GetType(B )), B)
                > End Function
                >
                > Private Function GetClassC(type As Type) As C
                > Return DirectCast(Acti vator.CreateIns tance(type), C)
                > End Function[/color]


                Hope this helps
                Jay

                "René Nordby" <rene@bluedot.d k> wrote in message
                news:exvUT3ndEH A.3044@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Hi All,
                >
                > Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
                > if anyone have any comments you are mostly welcome.
                >
                > Option Explicit On
                > Option Strict On
                >
                > Public Class A
                > Inherits B
                > End Class
                >
                > Public Class B
                > End Class
                >
                > Public Class C
                > Inherits A
                > 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
                >
                > Private Function GetClassC() As C
                > Return New C
                > End Function
                > End Class
                >
                > "René Nordby" <rene@bluedot.d k> wrote in message
                > news:%23WRqudnd EHA.3944@tk2msf tngp13.phx.gbl. ..[color=green]
                > > Absolutely, this is what I'm looking for, but as Jay writes, it doesn't[/color]
                > work[color=green]
                > > when it comes to runtime, so what should be done, to get it to work.
                > >
                > > I see what Jay means by returning the right object from start, but[/color][/color]
                that's[color=blue][color=green]
                > > not the solution, sorry.
                > >
                > > The above code is what I want, but how should it be implemented, so it
                > > works?
                > >
                > > René
                > >
                > > "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
                > > news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=darkred]
                > > > I just tried this and it works. Is this what you are looking[/color][/color]
                > or? -Carl.[color=green][color=darkred]
                > > >
                > > > 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.. .
                > > > > OK, thanks everyone for your replies.
                > > > >
                > > > > I will try to explain what I'm trying to do, and hopefully some one[/color]
                > > knows[color=darkred]
                > > > > how to do it.
                > > > >
                > > > > I want a Function to return a Class. With that Class in hand, I[/color][/color][/color]
                should[color=blue][color=green]
                > > be[color=darkred]
                > > > > 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. ..
                > > > > > 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[/color][/color]
                > (this[color=green][color=darkred]
                > > > > means
                > > > > > that I don't have other code in class B, than the Inherit[/color][/color]
                > statement).[color=green][color=darkred]
                > > > > >
                > > > > >
                > > > > >
                > > > > > Now I want to make a Type Casting on an object of the type A, so[/color][/color]
                > that[color=green][color=darkred]
                > > > > object
                > > > > > become a B type.
                > > > > >
                > > > > >
                > > > > >
                > > > > > I have tryied it, but gets the error 'System.Invalid CastException:
                > > > > Specified
                > > > > > cast is not valid.', so want am I missing.
                > > > > >
                > > > > >
                > > > > >
                > > > > > Please give a hint.
                > > > > >
                > > > > >
                > > > > >
                > > > > > René
                > > > > >
                > > > > >
                > > > > >
                > > > > >
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #23
                  Re: Type Casting

                  René,[color=blue]
                  > Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
                  > if anyone have any comments you are mostly welcome.[/color]
                  No you didn't, as your GetClassB method will fail with a
                  InvalidCastExce ption!
                  [color=blue][color=green]
                  > > The above code is what I want, but how should it be implemented, so it
                  > > works?[/color][/color]
                  It "cannot" be implemented in VS.NET 2003, as CType cannot change the type
                  of a C object into a B or A object. It can however Cast a B or A object in a
                  C variable to a B or A variable. Only predefined CTYPE conversions are
                  defined in VS.NET 2003, C to A & C to B are not predefined. Also see my
                  message to Nice chap about the difference between CType the conversion &
                  DirectCast the casting.

                  VS.NET 2005 (aka Whidbey, due out in 2005) you can overload CType such that
                  it can create a B or A object given a C object, HOWEVER!!! your sample seems
                  to be an extreme abuse of this ability! If you provide better details on
                  what you are really trying to accomplish we may be able to offer other
                  solutions then the two I offered earlier.

                  The correct way (only way really) to implement this is:

                  I would have GetClassA & GetClassB create the correct type of object, and
                  not include GetClass.

                  Alternatively you would need to pass a parameter to GetClass that indicates
                  what kind of object to actually create. I would either use an Enum or the
                  Type & Activator.Creat eInstance.
                  [color=blue]
                  > Public Function GetClassA() As A
                  > Return DirectCast(GetC lassC(gettype(A )), A)
                  > End Function
                  >
                  > Public Function GetClassB() As B
                  > Return DirectCast(GetC lassC(GetType(B )), B)
                  > End Function
                  >
                  > Private Function GetClassC(type As Type) As C
                  > Return DirectCast(Acti vator.CreateIns tance(type), C)
                  > End Function[/color]


                  Hope this helps
                  Jay

                  "René Nordby" <rene@bluedot.d k> wrote in message
                  news:exvUT3ndEH A.3044@TK2MSFTN GP10.phx.gbl...[color=blue]
                  > Hi All,
                  >
                  > Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
                  > if anyone have any comments you are mostly welcome.
                  >
                  > Option Explicit On
                  > Option Strict On
                  >
                  > Public Class A
                  > Inherits B
                  > End Class
                  >
                  > Public Class B
                  > End Class
                  >
                  > Public Class C
                  > Inherits A
                  > 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
                  >
                  > Private Function GetClassC() As C
                  > Return New C
                  > End Function
                  > End Class
                  >
                  > "René Nordby" <rene@bluedot.d k> wrote in message
                  > news:%23WRqudnd EHA.3944@tk2msf tngp13.phx.gbl. ..[color=green]
                  > > Absolutely, this is what I'm looking for, but as Jay writes, it doesn't[/color]
                  > work[color=green]
                  > > when it comes to runtime, so what should be done, to get it to work.
                  > >
                  > > I see what Jay means by returning the right object from start, but[/color][/color]
                  that's[color=blue][color=green]
                  > > not the solution, sorry.
                  > >
                  > > The above code is what I want, but how should it be implemented, so it
                  > > works?
                  > >
                  > > René
                  > >
                  > > "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
                  > > news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...[color=darkred]
                  > > > I just tried this and it works. Is this what you are looking[/color][/color]
                  > or? -Carl.[color=green][color=darkred]
                  > > >
                  > > > 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.. .
                  > > > > OK, thanks everyone for your replies.
                  > > > >
                  > > > > I will try to explain what I'm trying to do, and hopefully some one[/color]
                  > > knows[color=darkred]
                  > > > > how to do it.
                  > > > >
                  > > > > I want a Function to return a Class. With that Class in hand, I[/color][/color][/color]
                  should[color=blue][color=green]
                  > > be[color=darkred]
                  > > > > 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. ..
                  > > > > > 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[/color][/color]
                  > (this[color=green][color=darkred]
                  > > > > means
                  > > > > > that I don't have other code in class B, than the Inherit[/color][/color]
                  > statement).[color=green][color=darkred]
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > > Now I want to make a Type Casting on an object of the type A, so[/color][/color]
                  > that[color=green][color=darkred]
                  > > > > object
                  > > > > > become a B type.
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > > I have tryied it, but gets the error 'System.Invalid CastException:
                  > > > > Specified
                  > > > > > cast is not valid.', so want am I missing.
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > > Please give a hint.
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > > René
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • René Nordby

                    #24
                    Re: Type Casting

                    Thanks to all of you, especially Carl and Jay for your good and detailed
                    descriptions.

                    I think I have solved my problem, otherwise I will get back to you.

                    ..Net and OO is a fantastic world :-)

                    René

                    "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
                    news:urHOTv$dEH A.3212@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > Renee,
                    >
                    > The reason this works is because you are casting a derived object to a[/color]
                    base[color=blue]
                    > object. In your original post you stated you were trying to do just the
                    > opposite (cast a base object to a derived object), which as Jeff Johnson
                    > stated in the first response on this thread is not possible. ironic huh?
                    > :-)
                    >
                    > You can, in fact, cast a derived object to any one of it's bases. If that
                    > is what you wanted in the first place, then you have the solution and I
                    > guess we are done. If you still need to do it the other way around, give
                    > this a try. I experimented a good bit with this and it does work.
                    > Basically instead of converting the object (or casting it to a new[/color]
                    reference[color=blue]
                    > or whatever the right term is...) create a new object of the desired type
                    > and in its constructor, copy any properties or state information from the
                    > object you are converting from. Attached is a complete solution that
                    > illustrates what I am talking about. This example uses a base class C and
                    > two derived classes A and B. C has a method (or
                    > property) named ToA that changes itself into a type A object, and another
                    > property named ToB that changes itself into a type B object. So instead[/color]
                    of[color=blue]
                    > a procedure like this:
                    > CType(GetClassC , A)
                    > you do this:
                    > objC.ToA
                    >
                    > I hope either this helps, or that your own solution has already solved[/color]
                    your[color=blue]
                    > problem.
                    >
                    > -Carl
                    >
                    >
                    > "René Nordby" <rene@bluedot.d k> wrote in message
                    > news:exvUT3ndEH A.3044@TK2MSFTN GP10.phx.gbl...[color=green]
                    > > Hi All,
                    > >
                    > > Now I have solved it, but I'm not 100% sure that it is the rigt OO way,[/color][/color]
                    so[color=blue][color=green]
                    > > if anyone have any comments you are mostly welcome.
                    > >
                    > > Option Explicit On
                    > > Option Strict On
                    > >
                    > > Public Class A
                    > > Inherits B
                    > > End Class
                    > >
                    > > Public Class B
                    > > End Class
                    > >
                    > > Public Class C
                    > > Inherits A
                    > > 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
                    > >
                    > > Private Function GetClassC() As C
                    > > Return New C
                    > > End Function
                    > > End Class
                    > >
                    > > "René Nordby" <rene@bluedot.d k> wrote in message
                    > > news:%23WRqudnd EHA.3944@tk2msf tngp13.phx.gbl. ..[color=darkred]
                    > > > Absolutely, this is what I'm looking for, but as Jay writes, it[/color][/color][/color]
                    doesn't[color=blue][color=green]
                    > > work[color=darkred]
                    > > > when it comes to runtime, so what should be done, to get it to work.
                    > > >
                    > > > I see what Jay means by returning the right object from start, but[/color][/color]
                    > that's[color=green][color=darkred]
                    > > > not the solution, sorry.
                    > > >
                    > > > The above code is what I want, but how should it be implemented, so it
                    > > > works?
                    > > >
                    > > > René
                    > > >
                    > > > "Carl Tribble" <carltribble@sb cglobal.net> wrote in message
                    > > > news:ODBGspldEH A.3476@tk2msftn gp13.phx.gbl...
                    > > > > I just tried this and it works. Is this what you are looking[/color]
                    > > or? -Carl.[color=darkred]
                    > > > >
                    > > > > 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.. .
                    > > > > > OK, thanks everyone for your replies.
                    > > > > >
                    > > > > > I will try to explain what I'm trying to do, and hopefully some[/color][/color][/color]
                    one[color=blue][color=green][color=darkred]
                    > > > knows
                    > > > > > how to do it.
                    > > > > >
                    > > > > > I want a Function to return a Class. With that Class in hand, I[/color][/color]
                    > should[color=green][color=darkred]
                    > > > be
                    > > > > > 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. ..
                    > > > > > > 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[/color]
                    > > (this[color=darkred]
                    > > > > > means
                    > > > > > > that I don't have other code in class B, than the Inherit[/color]
                    > > statement).[color=darkred]
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > > > Now I want to make a Type Casting on an object of the type A, so[/color]
                    > > that[color=darkred]
                    > > > > > object
                    > > > > > > become a B type.
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > > > I have tryied it, but gets the error[/color][/color][/color]
                    'System.Invalid CastException:[color=blue][color=green][color=darkred]
                    > > > > > Specified
                    > > > > > > cast is not valid.', so want am I missing.
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > > > Please give a hint.
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > > > René
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > > >
                    > > > > >
                    > > > > >
                    > > > >
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >
                    >
                    >
                    >
                    >[/color]


                    Comment

                    Working...