Type Casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • René Nordby

    Type Casting

    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.Invalid CastException: Specified
    cast is not valid.', so want am I missing.



    Please give a hint.



    René




  • Jeff Johnson [MVP: VB]

    #2
    Re: Type Casting


    "René Nordby" <rene@bluedot.d k> wrote in message
    news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..
    [color=blue]
    > I have a class A and a class B, that 100% inherits from class A (this[/color]
    means[color=blue]
    > 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=blue]
    > become a B type.
    >
    >
    >
    > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
    Specified[color=blue]
    > cast is not valid.', so want am I missing.[/color]

    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.


    Comment

    • René Nordby

      #3
      Re: Type Casting

      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]" <i.get@enough.s pam> wrote in message
      news:%23FoWosZd EHA.2752@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      >
      > "René Nordby" <rene@bluedot.d k> wrote in message
      > news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..
      >[color=green]
      > > I have a class A and a class B, that 100% inherits from class A (this[/color]
      > means[color=green]
      > > 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=green]
      > > become a B type.
      > >
      > >
      > >
      > > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
      > Specified[color=green]
      > > cast is not valid.', so want am I missing.[/color]
      >
      > You're missing the fact that it can't be done. You can cast a derived[/color]
      class[color=blue]
      > into its base class, but you can't cast a base class to a derived class.
      > Someone correct me if I'm wrong.
      >
      >[/color]


      Comment

      • Nice Chap

        #4
        Re: Type Casting

        Hi Jeff,
        [color=blue]
        > You're missing the fact that it can't be done. You can cast a derived[/color]
        class[color=blue]
        > into its base class, but you can't cast a base class to a derived class.
        > Someone correct me if I'm wrong.[/color]

        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.


        Comment

        • René Nordby

          #5
          Re: Type Casting

          Could you please spell it out a little more?

          René

          "Nice Chap" <NiceChap@Plasm aDyne.com> wrote in message
          news:uk8cA4ZdEH A.4048@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi Jeff,
          >[color=green]
          > > You're missing the fact that it can't be done. You can cast a derived[/color]
          > class[color=green]
          > > into its base class, but you can't cast a base class to a derived class.
          > > Someone correct me if I'm wrong.[/color]
          >
          > You can indeed cast a base class reference to a sub-class reference and[/color]
          the[color=blue]
          > operation would result in a 'narrowing' conversion. BUT the conversion[/color]
          will[color=blue]
          > 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.
          >
          >[/color]


          Comment

          • Cor Ligthert

            #6
            Re: Type Casting

            René

            Have a look at this page.



            Maybe it makes some things clear.

            Cor


            Comment

            • Anon-E-Moose

              #7
              Re: Type Casting

              "Nice Chap" <NiceChap@Plasm aDyne.com> wrote in
              news:uk8cA4ZdEH A.4048@TK2MSFTN GP12.phx.gbl:
              [color=blue]
              > 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.[/color]

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

              Thanks.

              Comment

              • Tom Shelton

                #8
                Re: Type Casting

                In article <Xns9535AB438E3 6Danonemooseyah oocom@140.99.99 .130>, Anon-E-Moose wrote:[color=blue]
                > "Nice Chap" <NiceChap@Plasm aDyne.com> wrote in
                > news:uk8cA4ZdEH A.4048@TK2MSFTN GP12.phx.gbl:
                >[color=green]
                >> 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.[/color]
                >
                > ??? Can someone re-write this in clearer terms?
                >
                > Thanks.
                >[/color]

                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]

                Comment

                • Luhar

                  #9
                  Re: Type Casting

                  I believe the framework uses TypeConverters to cast between Strings &
                  Integer and vice versa. Google TypeConverter to get more info.

                  hth,

                  Luhar

                  "René Nordby" <rene@bluedot.d k> wrote in message
                  news:OFjuJzZdEH A.1048@tk2msftn gp13.phx.gbl...[color=blue]
                  > 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]" <i.get@enough.s pam> wrote in message
                  > news:%23FoWosZd EHA.2752@TK2MSF TNGP12.phx.gbl. ..[color=green]
                  > >
                  > > "René Nordby" <rene@bluedot.d k> wrote in message
                  > > news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..
                  > >[color=darkred]
                  > > > 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.[/color]
                  > >
                  > > You're missing the fact that it can't be done. You can cast a derived[/color]
                  > class[color=green]
                  > > into its base class, but you can't cast a base class to a derived class.
                  > > Someone correct me if I'm wrong.
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Carl Tribble

                    #10
                    Re: Type Casting

                    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" <rene@bluedot.d k> wrote in message
                    news:%23qOvulZd EHA.1356@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                    > 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=blue]
                    > 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=blue]
                    > become a B type.
                    >
                    >
                    >
                    > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
                    Specified[color=blue]
                    > cast is not valid.', so want am I missing.
                    >
                    >
                    >
                    > Please give a hint.
                    >
                    >
                    >
                    > René
                    >
                    >
                    >
                    >[/color]


                    Comment

                    • Nice Chap

                      #11
                      Re: Type Casting

                      Hi Tom,
                      [color=blue]
                      >Casting a baseclass reference to a subclass would be a widening[/color]
                      conversion, since you would be picking up methods/properties.<

                      Sorry, it is a 'Narrowing Conversion' ....


                      Comment

                      • René Nordby

                        #12
                        Re: Type Casting

                        OK, thanks everyone for your replies.

                        I will try to explain what I'm trying to do, and hopefully some one knows
                        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.

                        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=blue]
                        > 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=blue]
                        > 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=blue]
                        > become a B type.
                        >
                        >
                        >
                        > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
                        Specified[color=blue]
                        > cast is not valid.', so want am I missing.
                        >
                        >
                        >
                        > Please give a hint.
                        >
                        >
                        >
                        > René
                        >
                        >
                        >
                        >[/color]


                        Comment

                        • Jay B. Harlow [MVP - Outlook]

                          #13
                          Re: Type Casting

                          Nice Chap & Tom,
                          Casting a class from a base class or to a derived class is NOT a conversion!
                          So it can be neither Widening nor Narrowing!

                          It is a Cast.

                          A Conversion is physically changing the type of the object for example from
                          Integer to String, or String to Double.

                          A Cast the object stays the same type, however the variable it is placed
                          into changes. For example casting the sender parameter to the specific type
                          the object is. The object itself stays the same type however how you program
                          uses it changes.

                          You should use DirectCast for casting operations and use CType (or their
                          short cuts) for Conversions, for in VS.NET 2005 (aka Whidbey, due out
                          sometime in 2005) we can overload the CType operator to define how to
                          convert our class from one type to another (for example a Complex number
                          class can define how to convert itself to & from a Double).

                          Hope this helps
                          Jay


                          "Nice Chap" <NiceChap@Plasm aDyne.com> wrote in message
                          news:uTW0mFkdEH A.3988@tk2msftn gp13.phx.gbl...[color=blue]
                          > Hi Tom,
                          >[color=green]
                          > >Casting a baseclass reference to a subclass would be a widening[/color]
                          > conversion, since you would be picking up methods/properties.<
                          >
                          > Sorry, it is a 'Narrowing Conversion' ....
                          >
                          >[/color]


                          Comment

                          • Carl Tribble

                            #14
                            Re: Type Casting

                            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=blue]
                            > OK, thanks everyone for your replies.
                            >
                            > I will try to explain what I'm trying to do, and hopefully some one knows
                            > 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.
                            >
                            > 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=green]
                            > > 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=green]
                            > > 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=green]
                            > > become a B type.
                            > >
                            > >
                            > >
                            > > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
                            > Specified[color=green]
                            > > cast is not valid.', so want am I missing.
                            > >
                            > >
                            > >
                            > > Please give a hint.
                            > >
                            > >
                            > >
                            > > René
                            > >
                            > >
                            > >
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: Type Casting

                              René,
                              I would have GetClassA & GetClassB create the correct type of object, and
                              not include GetClass.
                              [color=blue]
                              > Sub GetClassA[/color]
                              myClassA = New ClassA[color=blue]
                              > End Sub
                              >
                              > Sub GetClassB[/color]
                              myClassB = New ClassB[color=blue]
                              > End Sub[/color]

                              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.

                              Hope this helps
                              Jay



                              "René Nordby" <rene@bluedot.d k> wrote in message
                              news:%23gDRMild EHA.212@TK2MSFT NGP12.phx.gbl.. .[color=blue]
                              > OK, thanks everyone for your replies.
                              >
                              > I will try to explain what I'm trying to do, and hopefully some one knows
                              > 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.
                              >
                              > 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=green]
                              > > 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=green]
                              > > 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=green]
                              > > become a B type.
                              > >
                              > >
                              > >
                              > > I have tryied it, but gets the error 'System.Invalid CastException:[/color]
                              > Specified[color=green]
                              > > cast is not valid.', so want am I missing.
                              > >
                              > >
                              > >
                              > > Please give a hint.
                              > >
                              > >
                              > >
                              > > René
                              > >
                              > >
                              > >
                              > >[/color]
                              >
                              >[/color]


                              Comment

                              Working...