APAX, Option Strict and casting System.Object to System.Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • m830266@yahoo.co.uk

    #1

    APAX, Option Strict and casting System.Object to System.Array

    I'm trying to use the APAX serial I/O control
    (www.turbocontrol.com/AProZilla.htm) in a VB.NET project and I'm having
    trouble with its 'data received' events. The data is supplied by APAX
    as a variant which appears to VB.NET as a System.Object. I want to
    convert this to a System.Array so that I can iterate over its elements.

    The VB6 usage is described at
    www.turbocontrol.com/TechTips/20020426.htm. If I try something similar
    in VB.NET, namely

    Private Sub AxApax1_OnRXD(B yVal sender As Object, ByVal e As
    AxApax1.IApaxEv ents_OnRXDEvent ) Handles AxApax1.OnRXD

    Dim i As Integer

    For i = LBound(e.data) To UBound(e.data)
    Debug.Writeline (e.data(i))
    Next
    End Sub

    then the IDE underlines each occurrence of e.data and says "Option
    Strict On disallows implicit conversion from System.Object to
    System.Array". If I turn Option Strict off, everything works fine and
    my serial data is printed to the Output window.

    I've tried things like

    Dim data() As Byte = DirectCast(e.da ta, Byte())

    For Each b As Byte In data
    Debug.Writeline (b)
    Next

    and CType instead of DirectCast, but although this compiles OK
    execution doesn't seem to get beyond the Dim data() line! No error or
    exception is raised. If I put a breakpoint on this line, execution
    stops when data is received and I've checked that e.data does indeed
    contain the serial data I'm sending, but if I hit F8 to step to the For
    Each line, the IDE seems to step out of this event handler and the loop
    is not executed. No data is printed to the Output window and execution
    breaks again on the Dim data() line when more data is received.

    The other weird thing is that if I type '?e.data' in the Command
    window, it tells me that e.data *is* already a System.Array:

    ?e.data
    {System.Array}
    (&H1): &H41
    (&H2): &H2C
    (&H3): &H2C
    [...]

    So why is the IDE complaining? Any idea what's going on, and how I can
    get this cast to work? I'm using VS.NET 2003 v7.1.3088 under Win2K
    SP4.

    Mike.

  • Larry Lard

    #2
    Re: APAX, Option Strict and casting System.Object to System.Array


    Interesting problem :)

    m830266@yahoo.c o.uk wrote:[color=blue]
    > I'm trying to use the APAX serial I/O control
    > (www.turbocontrol.com/AProZilla.htm) in a VB.NET project and I'm having
    > trouble with its 'data received' events. The data is supplied by APAX
    > as a variant which appears to VB.NET as a System.Object.[/color]

    Remember Variant is a COM type which means nothing to .NET.
    [color=blue]
    > I want to
    > convert this to a System.Array so that I can iterate over its elements.
    >
    > The VB6 usage is described at
    > www.turbocontrol.com/TechTips/20020426.htm. If I try something similar
    > in VB.NET, namely
    >
    > Private Sub AxApax1_OnRXD(B yVal sender As Object, ByVal e As
    > AxApax1.IApaxEv ents_OnRXDEvent ) Handles AxApax1.OnRXD
    >
    > Dim i As Integer
    >
    > For i = LBound(e.data) To UBound(e.data)
    > Debug.Writeline (e.data(i))
    > Next
    > End Sub
    >
    > then the IDE underlines each occurrence of e.data and says "Option
    > Strict On disallows implicit conversion from System.Object to
    > System.Array". If I turn Option Strict off, everything works fine and
    > my serial data is printed to the Output window.[/color]

    OK, you get this error because LBound() and UBound() require their
    parameter to be a System.Array, so this is why the compiler complains.
    [color=blue]
    >
    > I've tried things like
    >
    > Dim data() As Byte = DirectCast(e.da ta, Byte())
    >
    > For Each b As Byte In data
    > Debug.Writeline (b)
    > Next
    >
    > and CType instead of DirectCast, but although this compiles OK
    > execution doesn't seem to get beyond the Dim data() line! No error or
    > exception is raised. If I put a breakpoint on this line, execution
    > stops when data is received and I've checked that e.data does indeed
    > contain the serial data I'm sending, but if I hit F8 to step to the For
    > Each line, the IDE seems to step out of this event handler and the loop
    > is not executed. No data is printed to the Output window and execution
    > breaks again on the Dim data() line when more data is received.[/color]

    That's quite odd.
    [color=blue]
    >
    > The other weird thing is that if I type '?e.data' in the Command
    > window, it tells me that e.data *is* already a System.Array:
    >
    > ?e.data
    > {System.Array}
    > (&H1): &H41
    > (&H2): &H2C
    > (&H3): &H2C
    > [...]
    >
    > So why is the IDE complaining? Any idea what's going on, and how I can
    > get this cast to work? I'm using VS.NET 2003 v7.1.3088 under Win2K
    > SP4.[/color]

    Aha! Here we have a clue at last. Look at that debug output: What's the
    first element of the array? It's array(1). Now, I'm not sure on the
    exact details, but I know that all 'native' framework arrays have
    *zero-based* indices. The fact that your array has a *one-based* index
    might well be the reason that you are having trouble dealing with it.

    As to why the debugger can work out what it is, that is strange indeed.

    My advice would be to get to that breakpoint again, have a look at the
    docs for System.Array, and play around in the command window to see if
    you can get maybe .GetValue to work or something. Sorry I can't offer
    anything more concrete.

    --
    Larry Lard
    Replies to group please

    Comment

    Working...