Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

CWArray in VB.NET

I've just tried using CWArray.IndexArray to index a column out of a 2D
array, and although it's a doddle in VB6 I can't get it to run in VB.NET.
Whatever I try, I get the message "Run-time exception thrown :
System.Runtime.InteropServices.COMException - The data type is not correct."

The 2D array I'm feeding in is a 2D array of singles. The index array is a
1D array of longs. To simplify things I have tried not assigning the result
to anything, so the immediate problem is in those two variables.

Can CWArray.IndexArray be made to work with VB.NET or is this a lost cause?

--
Dr. Craig Graham, Software Engineer
Advanced Analysis and Integration Limited, UK. http://www.aail.co.uk/
0 Kudos
Message 1 of 7
(5,846 Views)
The trick is figuring out which .NET types to use that will match up with what the underlying COM implementation is expecting after the .NET data types are marshaled to unmanaged code. I haven't seen your code, but from what you're describing it sounds like there are two potential marshaling problems: a.) the type of the index array and b.) what to use for NULL when you want to extract a range of a given dimension. Try using an array of objects instead of an array of longs for a.) and try using DBNull.Value for b.). For example, here's a VB.NET equivalent of the example in the IndexArray documentation that will work with the interop interfaces for CWArray (note the loops use syntax that was added in Visual Studio .NET 2003, so you may need to adjust it if you'
re still using Visual Studio .NET 2002):

Dim a(4, 5) As Double

Dim l As Integer = 0
For j As Integer = 0 To 4
For k As Integer = 0 To 5
a(j, k) = l
l += 1
Next
Next

Dim result() As Double

' Result is a 6-element array { 12, 13, 14, 15, 16, 17 }
result = AxCWArray1.IndexArray(a, New Object() {2, DBNull.Value})

' Result is a 5-element array { 1, 7, 13, 19, 25 }
result = AxCWArray1.IndexArray(a, New Object() {DBNull.Value, 1})

Please reply to let us know if this helped resolve your issue. If not, please post sample code that reproduces your problem. Thanks.

- Elton
0 Kudos
Message 2 of 7
(5,846 Views)
"Elton Wells" wrote in message
news:506500000005000000190F0100-1042324653000@exchange.ni.com...
> The trick is figuring out which .NET types to use that will match up
> with what the underlying COM implementation is expecting after the
> NET data types are marshaled to unmanaged code. I haven't seen your
> code, but from what you're describing it sounds like there are two
> potential marshaling problems: a.) the type of the index array and b.)
> what to use for NULL when you want to extract a range of a given
> dimension. Try using an array of objects instead of an array of longs
> for a.) and try using DBNull.Value for b.). For example, here's a
> VB.NET equivalent of the example in the IndexArray documentation that
> will work with th
e interop interfaces for CWArray (note the loops use
> syntax that was added in Visual Studio .NET 2003, so you may need to
> adjust it if you're still using Visual Studio .NET 2002):

Thanks for the response.

I figured there was a problem with types, and by declaring integers for the
indices I was able to get it to run without an error- however I was only
able to get it to return single values, rather than an array, since I could
not figure out how to set an element of an integer array to "NULL"; both
"nothing" and "vbNull" were interpreted as zero. Declaring an array of
objects and populating it with an integer and a nothing also returned only a
single element, so I gave up on it. for some reason I could not use
"DBNull.value" but I can't recall the error I got on building. I'll file
this message away for next time I have to try and use cwarray.

I found a property of the .NET array object that allowed me to split out
single columns of a 2D array- luckily it worked the way I wanted
it to since
there is no option to transpose the operation- but it does the job for the
moment.

--
Dr. Craig Graham, Software Engineer
Advanced Analysis and Integration Limited, UK. http://www.aail.co.uk/
0 Kudos
Message 3 of 7
(5,845 Views)
I'm curious why you could not use DBNull.Value. You actually got an error at build-time when trying to use this? Did you try building the example I posted above or was this in your code? If the latter, could you please post the code if you still have it?

Also, just out of curiosity ... what is the property that you're using on the .NET array type that allows you to split out single columns of a 2D array? Could you please post an example of how you do this with .NET arrays?

- Elton
0 Kudos
Message 4 of 7
(5,845 Views)
Alas I don't have the code now, but it was simple enough to reproduce. It
appears I tried system.dbnull.value with an array of singles, and the
problem goes away if I use an array of objects. Since I have that code
working now, though, I'm not goping to go back and start playing with
cwarray again for the moment.

As for the .NET method;

Array.Copy(retArray, 0, singleColumn, 0, samples)

This will copy the first column of retArray to the first column of
SingleColumn, where both are 2D arrays and "samples" is the number of rows.
The two zeroes are the offsets. By changing the first offset to be a
multiple of samples, you can extract any column of retArray into the first
column of singleColumn, since the array is treated as the 1D conc
atenation
of the columns of the 2D array.

I can't remember offhand which index is the "column" and which is the "row"
but a bit of testing will show it if it's needed 🙂

--
Dr. Craig Graham, Software Engineer
Advanced Analysis and Integration Limited, UK. http://www.aail.co.uk/

"Elton Wells" wrote in message
news:506500000005000000C50F0100-1042324653000@exchange.ni.com...
> I'm curious why you could not use DBNull.Value. You actually got an
> error at build-time when trying to use this? Did you try building the
> example I posted above or was this in your code? If the latter, could
> you please post the code if you still have it?
>
> Also, just out of curiosity ... what is the property that you're using
> on the .NET array type that allows you to split out single columns of
> a 2D array? Could you please post an example of how you do this with
> NET arrays?
>
> - Elton
0 Kudos
Message 5 of 7
(5,846 Views)
OK, I see ... I knew about Array.Copy, but I tend to think of multidimensional arrays as rows that are conceptually laid out end to end, hence the first index represents the row, but it's really open to interpretation. If you think of the first index as the column, then Array.Copy works very well and is the best solution for this in VB.NET.

- Elton
0 Kudos
Message 6 of 7
(5,844 Views)
This is how I think. I was quite pleasantly suprised to find I can get
arrays back from nidmm.dll that are arranged as concatenated columns- there
must be some rearrangement going on before the data get sent back, since I
don't see how a point can be appended during scanning unless the array is
arranged initially as concatenated rows.

--
Dr. Craig Graham, Software Engineer
Advanced Analysis and Integration Limited, UK. http://www.aail.co.uk/

"Elton Wells" wrote in message
news:5065000000050000002D100100-1042324653000@exchange.ni.com...
> OK, I see ... I knew about Array.Copy, but I tend to think of
> multidimensional arrays as rows that are conceptually laid out end to
> end, hence the first index repr
esents the row, but it's really open to
> interpretation. If you think of the first index as the column, then
> Array.Copy works very well and is the best solution for this in
> VB.NET.
>
> - Elton
0 Kudos
Message 7 of 7
(5,844 Views)