473,761 Members | 5,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Nov 21 '05 #1
1 3771

Interesting problem :)

m8*****@yahoo.c o.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(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.
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.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.
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
1144
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 strict to off, but with it on I get the error: Option Strict On disallows implicit conversions from 'System.Object' to '1-dimensional array of Single'. Dim myValues As Object Dim mySngArr() As Single
0
301
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 strict to off, but with it on I get the error: Option Strict On disallows implicit conversions from 'System.Object' to '1-dimensional array of Single'. Dim myValues As Object Dim mySngArr() As Single
8
1829
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 record in a sql Server view) .... Dim entry As Domino.NotesViewEntry Dim obj As Object str1 = entry.ColumnValues(0)
13
2515
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 original value...To accommodate all variable types I made the arguments in ChangeValue() of type object...I then check the typecode and do the correct comparison etc... With Option Strict ON I have to cast the arguments to the generic object...
6
1574
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 build event handlers? Thanks, Brett
15
1548
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 a good thing. i have now been asked to debug a vb2005 web app for 3 weeks and there is no mention of option strict in it, (there are also no classes defined, just a couple of structures) everything is define as 'as object', data coming back...
9
5683
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 List() As String) As String() ' Returns the unique values of in array, in an array. Dim Temp As New System.Collections.Specialized.StringCollection()
18
3637
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 - 01,85,78,15 etc... Is there some sort of easy one liner that does this? Hard to search the help files for anything that would assist me in doing this. Thanks in advance.
14
1770
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
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.