DirectCast with unkown type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tubs

    DirectCast with unkown type

    i am attempting to write something which can morph itself to whatever
    comes in and get the value property from it but i don't know what type
    it is until runtime. I am therefore trying to use reflection and get
    the type from there and then cast it to that type so i can use it.
    When i use DirectCast, it won't let me do something like
    DirectCast(myBi nding.Control,m yBinding.Contro l.GetType()), in fact
    intellisense doesn't work at all because it is looking for a type
    ONLY. Isn't there a way to late cast this?
  • Nice Chap

    #2
    Re: DirectCast with unkown type

    DirectCast is strictly for cases when the Source and Target Types are fixed
    and also don't need coercion. CType an option ??


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: DirectCast with unkown type

      Tubs,
      If you do not know the type of the variable you are assigning to at compile
      time, you cannot use DirectCast or CType! As both require that you know the
      actual type at compile time.

      Can you give a more complete example of what you are attempting? (10 to 15
      lines of code).

      Late Binding (Option Strict Off) or CallByName may be an option instead of
      Reflection, as both Late Binding & CallByName wrap reflection for you.

      Hope this helps
      Jay


      "Tubs" <tstauffer@comm andalkon.com> wrote in message
      news:449db0f1.0 311120449.1a453 3af@posting.goo gle.com...[color=blue]
      > i am attempting to write something which can morph itself to whatever
      > comes in and get the value property from it but i don't know what type
      > it is until runtime. I am therefore trying to use reflection and get
      > the type from there and then cast it to that type so i can use it.
      > When i use DirectCast, it won't let me do something like
      > DirectCast(myBi nding.Control,m yBinding.Contro l.GetType()), in fact
      > intellisense doesn't work at all because it is looking for a type
      > ONLY. Isn't there a way to late cast this?[/color]


      Comment

      • Troy Stauffer

        #4
        Re: DirectCast with unkown type

        unfortunately not. same thing

        Troy B. Stauffer
        Jack of all trades, master of none :)

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • One Handed Man

          #5
          Re: DirectCast with unkown type


          Cant you do a select case and then say

          Case . . . .
          'cast your type

          Case . . .

          ?


          OHM



          Tubs wrote:[color=blue]
          > i am attempting to write something which can morph itself to whatever
          > comes in and get the value property from it but i don't know what type
          > it is until runtime. I am therefore trying to use reflection and get
          > the type from there and then cast it to that type so i can use it.
          > When i use DirectCast, it won't let me do something like
          > DirectCast(myBi nding.Control,m yBinding.Contro l.GetType()), in fact
          > intellisense doesn't work at all because it is looking for a type
          > ONLY. Isn't there a way to late cast this?[/color]


          Comment

          • Tom Shelton

            #6
            Re: DirectCast with unkown type

            In article <449db0f1.03111 20449.1a4533af@ posting.google. com>, Tubs wrote:[color=blue]
            > i am attempting to write something which can morph itself to whatever
            > comes in and get the value property from it but i don't know what type
            > it is until runtime. I am therefore trying to use reflection and get
            > the type from there and then cast it to that type so i can use it.
            > When i use DirectCast, it won't let me do something like
            > DirectCast(myBi nding.Control,m yBinding.Contro l.GetType()), in fact
            > intellisense doesn't work at all because it is looking for a type
            > ONLY. Isn't there a way to late cast this?[/color]

            Like Jay said, post a little code as to what your trying to
            accomplish... But, I have to ask - are these classes that you have
            control over? Because if they are - this may be a good place to
            implement an interface...

            --
            Tom Shelton
            MVP [Visual Basic]

            Comment

            • Troy Stauffer

              #7
              Re: DirectCast with unkown type

              Thanks guys but i am now going at it a little differently. What i was
              trying to do, which i guess i would still like to know if it is
              possible, is:

              within the binding.parse event of a binding get at the bound controls
              property values. The problem i has was that i needed to be able to cast
              that control to the one that it was, which the binder knows, so i could
              use its interface but using object also works. I wonder though just how
              this works because essentially don't they have to do the same thing. I
              WAS able to get CallByName to work instead as well and maybe that is
              what they are really doing underneath.

              Example:
              dim MyControl as MySpecialContro l = directcast(MyBi nding.Control,
              MySpecialContro l)
              'Problem here is that it could be one of many derived classes so i need
              to cast it to that particular class
              'Using Object solved this or CallByName but i am still bummed why
              DirectCast can't do it
              debug.print MyControl.Speci alProperty

              Troy B. Stauffer
              Jack of all trades, master of none :)

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: DirectCast with unkown type

                Troy,
                It sounds like you do "know" the type of variables you are working with.

                I normally use the TypeOf Is operator, something like:

                Dim ctl As Control = myBinding.Contr ol
                If TypeOf ctl Is TextBox then
                Dim text As TextBox = DirectCast(ctl, TextBox)
                ElseIf TypeOf ctl Is RadioButton Then
                Dim radio As RadioButton = DirectCast(ctl, RadioButton)

                ElseIf TypeOf ctl Is MySpecialContro l Then
                dim MyControl as MySpecialContro l = directcast(MyBi nding.Control,
                MySpecialContro l)
                ElseIf...
                End If
                [color=blue]
                > 'Using Object solved this or CallByName but i am still bummed why
                > DirectCast can't do it[/color]
                As I show above, DirectCast can do it!

                Hope this helps
                Jay
                "Troy Stauffer" <tstauffer@comm andalkon.com> wrote in message
                news:OrNVJhfqDH A.2820@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Thanks guys but i am now going at it a little differently. What i was
                > trying to do, which i guess i would still like to know if it is
                > possible, is:
                >
                > within the binding.parse event of a binding get at the bound controls
                > property values. The problem i has was that i needed to be able to cast
                > that control to the one that it was, which the binder knows, so i could
                > use its interface but using object also works. I wonder though just how
                > this works because essentially don't they have to do the same thing. I
                > WAS able to get CallByName to work instead as well and maybe that is
                > what they are really doing underneath.
                >
                > Example:
                > dim MyControl as MySpecialContro l = directcast(MyBi nding.Control,
                > MySpecialContro l)
                > 'Problem here is that it could be one of many derived classes so i need
                > to cast it to that particular class
                > 'Using Object solved this or CallByName but i am still bummed why
                > DirectCast can't do it
                > debug.print MyControl.Speci alProperty
                >
                > Troy B. Stauffer
                > Jack of all trades, master of none :)
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                > Don't just participate in USENET...get rewarded for it![/color]


                Comment

                • Troy Stauffer

                  #9
                  Re: DirectCast with unkown type

                  Thanks, i guess i just hate doing that because if you ever add new ones
                  into the fold, you have to code them specifically, whereas a truly
                  dynamic solution would just pick up on it. Not only are my controls not
                  know but which property to look at is not known. So a truly dynamic
                  solution would just warm my heart. Thanks for the post back anyhow

                  Troy B. Stauffer
                  Jack of all trades, master of none :)

                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: DirectCast with unkown type

                    Troy,
                    You have the "truly dynamic solution": Late Binding, Reflection or
                    CallByName! (which are all based on Reflection).

                    DirectCast & CType are not intended for "truly dynamic solutions".
                    DirectCast is intended for where you know the type of the object, but that
                    object is in a variable of a different type, normally this different type
                    will be a base type. CType is there for when you an object of one type and
                    you want to convert it to an object of a different type (think of CType as
                    Convert Type).

                    DirectCast & CType are there to avoid the "truly dynamic solution"! As the
                    "Truly dynamic solution" is expensive at runtime.

                    The other option to avoid the overhead of Reflection is to use an Interface,
                    which someone else suggested, which is the route I would normally take! The
                    interface avoids "you have to code them specifically" as it delegates that
                    code to each class (encapsulation) . This of course assumes that each control
                    "binds" to a specific property of that control. Of course if each control
                    has multiple properties that could be bound to, this won't work as well...

                    Public Interface TheChanger
                    Public Property TheProperty As String
                    End Interface

                    Public Class SpecialControl1
                    Implements TheChanger

                    Public Property TheProperty() As String Implements
                    TheChanger.TheP roperty
                    Get
                    Return Me.Text
                    End Get
                    Set(ByVal value As String)
                    Me.Text = value
                    End Set
                    End Property

                    End Class

                    Public Class SpecialControl2
                    Implements TheChanger

                    Public Property TheProperty() As String Implements
                    TheChanger.TheP roperty
                    Get
                    Return Me.Widget.ToStr ing
                    End Get
                    Set(ByVal value As String)
                    Me.Widget = Widget.FromStri ng(value)
                    End Set
                    End Property

                    End Class

                    BTW: Is there a reason you are attempting on building your own binding
                    mechanism on top of the .NET binding mechanism. What are you needing that
                    the .NET binding does not provide?

                    Hope this helps
                    Jay

                    "Troy Stauffer" <tstauffer@comm andalkon.com> wrote in message
                    news:u0wHyCgqDH A.2488@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Thanks, i guess i just hate doing that because if you ever add new ones
                    > into the fold, you have to code them specifically, whereas a truly
                    > dynamic solution would just pick up on it. Not only are my controls not
                    > know but which property to look at is not known. So a truly dynamic
                    > solution would just warm my heart. Thanks for the post back anyhow
                    >
                    > Troy B. Stauffer
                    > Jack of all trades, master of none :)
                    >
                    > *** Sent via Developersdex http://www.developersdex.com ***
                    > Don't just participate in USENET...get rewarded for it![/color]


                    Comment

                    • Troy Stauffer

                      #11
                      Re: DirectCast with unkown type

                      heh, interesting question. This whole debacle started because i wanted
                      to create and automatically bound control. This control is based on
                      something i called IOBasePoint. You can then derive and IOAnalogPoint
                      and an IODigitalPoint from it. DigitalPoint has a value which is type
                      boolean and AnalogPoint has value of single. Now add binding into the
                      picture. First of all, if the value is ever null coming from the bind
                      then it blows chunks. So i finally figured out that by trapping the
                      Parse and Format functions of the binding that i could help before the
                      actual binding happens. Well it doesn't stop there. In the cast that
                      the value coming in is bogus (i.e. a string for the AnalogValue) then i
                      want to be able to avoid the bind. From everything i can figure out,
                      there is no way to STOP the binding from happening. All you can do is
                      change the value. (Is there a cancel ???) Anyhow, in the case where the
                      new value is bogus, i want to set it to the old value, which i need to
                      them interrogate the control in question and get the value of the bound
                      property and set the bind value to that giving the apppearance that the
                      value didn't change. Sounds like a mess huh. I think it is.
                      One last thing. I also have a binding condition i put on which says to
                      only allow the bind to happen if the condition is true. For this i
                      actually build an expression evaulator which uses the codedom, spits out
                      code and compiles to an In Memory DLL which i then call. If that fails,
                      then i also have to keep the value the same (again in need of a cancel
                      here)

                      THoughts of a better way??

                      Troy B. Stauffer
                      Jack of all trades, master of none :)

                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: DirectCast with unkown type

                        Troy,
                        I would consider something like:

                        Public MustInherit Class IOBasePoint
                        Public MustOverride Property BoundValue() As Object
                        End Class

                        Public Class IODigitalPoint
                        Inherits IOBasePoint

                        Public Value As Boolean

                        Public Overrides Property BoundValue As Object
                        Get
                        Return Value
                        End Get
                        Set(ByVal value As Object)
                        If Value is Nothing Then
                        ' handle the nothing case
                        End If
                        ' any other validation Analog Point may need
                        Me.Value = CType(value, Boolean)
                        End Set
                        End Property

                        End Class

                        Public Class IOAnalogPoint
                        Inherits IOBasePoint

                        Public Value As Single

                        Public Overrides Property BoundValue As Object
                        Get
                        Return Value
                        End Get
                        Set(ByVal value As Object)
                        If Value is Nothing Then
                        ' handle the nothing case
                        End If
                        ' any other validation Analog Point may need
                        Me.Value = CType(value, Single)
                        End Set
                        End Property

                        End Class

                        Then I would bind to IOBasePoint.Bou ndValue, each derived class has the any
                        validation needed in its BoundValue setter.

                        Is IOBasePoint a control on the screen or a "data" object that you are
                        binding to?

                        Using the above I'm not so sure you would need the Parse & Format events...

                        Hope this helps
                        Jay

                        "Troy Stauffer" <tstauffer@comm andalkon.com> wrote in message
                        news:%23L7YHVhq DHA.2964@tk2msf tngp13.phx.gbl. ..[color=blue]
                        > heh, interesting question. This whole debacle started because i wanted
                        > to create and automatically bound control. This control is based on
                        > something i called IOBasePoint. You can then derive and IOAnalogPoint
                        > and an IODigitalPoint from it. DigitalPoint has a value which is type
                        > boolean and AnalogPoint has value of single. Now add binding into the
                        > picture. First of all, if the value is ever null coming from the bind
                        > then it blows chunks. So i finally figured out that by trapping the
                        > Parse and Format functions of the binding that i could help before the
                        > actual binding happens. Well it doesn't stop there. In the cast that
                        > the value coming in is bogus (i.e. a string for the AnalogValue) then i
                        > want to be able to avoid the bind. From everything i can figure out,
                        > there is no way to STOP the binding from happening. All you can do is
                        > change the value. (Is there a cancel ???) Anyhow, in the case where the
                        > new value is bogus, i want to set it to the old value, which i need to
                        > them interrogate the control in question and get the value of the bound
                        > property and set the bind value to that giving the apppearance that the
                        > value didn't change. Sounds like a mess huh. I think it is.
                        > One last thing. I also have a binding condition i put on which says to
                        > only allow the bind to happen if the condition is true. For this i
                        > actually build an expression evaulator which uses the codedom, spits out
                        > code and compiles to an In Memory DLL which i then call. If that fails,
                        > then i also have to keep the value the same (again in need of a cancel
                        > here)
                        >
                        > THoughts of a better way??
                        >
                        > Troy B. Stauffer
                        > Jack of all trades, master of none :)
                        >
                        > *** Sent via Developersdex http://www.developersdex.com ***
                        > Don't just participate in USENET...get rewarded for it![/color]


                        Comment

                        Working...