Question

I have an application in VBA that gives to my VB.Net dll one bi-dimensional variant. It is a an array, in which every component is a another array containing two positions.

I want to get this two-position array. When I am using VBA I can access directly the data from each position by doing:

dataArray(index, 0) or dataArray(index, 1)

And when I want to get the two-position array I can use:

Dim posArray as variant
posArray = dataArray(index)

posArray(0) contains dataArray(index, 0) and posArray(1) contains dataArray(index, 1).

But when I am using VB.Net, I can access directly the data, just like the first example. However, when I try to get one dimension from the two-dimension array, like I did in the second example, it is not possible.

Dim posArray as Object
posArray = dataArray(index)

It says "Attempted to operate on an array with the incorrect number of dimensions."

I have tried everything to make it work, and I don't want to make the attribution one by one, like:

posArray(0) = dataArray(index, 0)
posArray(1) = dataArray(index, 1)

Thank you for the help.

Was it helpful?

Solution

You'll need to spin through and generate the 1D array yourself. There's no library call in .NET to do it for you.

dim arraySlice as string[yourArrayWidth]
for index = 0 to yourArrayWidth
  arraySlice[index] = yourArray[4, index]
next

The example above would grab row 4 out of yourArray and stick it into arraySlice for you. Naturally, you'll want to clean that up and put it into a function that accepts a rowIndex as a parameter (and another function for splitting vertically that accepts a columnIndex).

It's little functions like this that you'll collect over the years into your own utility library. 5 years from now, you'll need an ArraySlice and you'll already have a function to do it.

OTHER TIPS

Dim Dim1Len As Integer = 5
Dim Dim2Len As Integer = 2
Dim DimToExtract As Integer = 2
Dim inMultiDArr(5, 2) As Integer
inMultiDArr = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}
Dim outSingleDArr(5) As Integer
outSingleDArr = {0, 0, 0, 0, 0}

Buffer.BlockCopy(inMultiDArr, Marshal.SizeOf(GetType(Integer)) * Dim1Len * (DimToExtract - 1),
                 outSingleDArr, 0, Marshal.SizeOf(GetType(Integer)) * Dim1Len)

After BlockCopy the 'outSingleDArr' will contain { 6, 7, 8, 9, 10 }. The above was derived from this post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top