473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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(ByVal sender As Object, ByVal e As
AxApax1.IApaxEvents_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.data, 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.

Nov 21 '05 #1
1 3751

Interesting problem :)

m8*****@yahoo.co.uk wrote:
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.
Remember Variant is a COM type which means nothing to .NET.
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(ByVal sender As Object, ByVal e As
AxApax1.IApaxEvents_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.
OK, you get this error because LBound() and UBound() require their
parameter to be a System.Array, so this is why the compiler complains.

I've tried things like

Dim data() As Byte = DirectCast(e.data, 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.
That's quite odd.

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.


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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JerryH | last post by:
I'm using a 3rd party dll to retrieve some data (I have no choice in this!), this returns an Object variable that I know contains an array of singles. The following code works fine if I set option...
0
by: JerryH | last post by:
I'm using a 3rd party dll to retrieve some data (I have no choice in this!), this returns an Object variable that I know contains an array of singles. The following code works fine if I set option...
8
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
13
by: Shannon Richards | last post by:
Hello: I have a problem using ByRef arguments with Option Strict ON. I have built a generic sub procedure "ChangeValue()" to change the value of an argument if the new value is not the same as the...
6
by: Brett | last post by:
I find there is more casting required in C# than VB.NET. If Option Strict/Explicit is turned on, will this basically create the same environment as C# - uppercase, more casting required, must...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
18
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.