|
-
Oct 20th, 2011, 06:11 PM
#1
Thread Starter
Hyperactive Member
Array dimensions problem from VB6 to .NET
Hi all, I'm upgrading my VB6 project to VB.NET and I found one of the problem below relating to dimensional array.
Here's my VB6 coding :
Code:
Private Function ConvertMatrixToBase0(ByVal MyMat() As Double) As Double()
Dim i, j As Long
Dim ConvMat() As Double
On Error GoTo ErrHandler_ConvertMatrixToBase0
If MultiDimensional(MyMat) = False Then '1-dimensional array
ReDim ConvMat(UBound(MyMat, 1) - 1)
For i = 1 To UBound(MyMat, 1)
ConvMat(i - 1) = MyMat(i)
Next i
ConvertMatrixToBase0 = ConvMat
Else 'multi-dimensional array
ReDim ConvMat(UBound(MyMat, 1) - 1, UBound(MyMat, 2) - 1)
For i = 1 To UBound(MyMat, 1)
For j = 1 To UBound(MyMat, 2)
ConvMat(i - 1, j - 1) = MyMat(i, j)
Next j
Next i
ConvertMatrixToBase0 = ConvMat
End If
Exit Function
ErrHandler_ConvertMatrixToBase0:
End Function
When i port it over to VB.NET, i encountered error message 'ReDim' cannot change the number of dimensions of an array. for the line highlighted in red. And I suspect that VB.NET need to always define the exact dimension of the array during the declaration. But for the function above, for the array of 'MyMat()', I do not know what is the exact dimension every time it runs, and that is why i need to check the dimension of the array.
My question is do we have any solution in dealing unknown dimension array in VB.NET?
As in VB6, we can just define Array() instead of Array (,) in VB.NET.
I'm still on the path of learning.... 
-
Oct 20th, 2011, 07:48 PM
#2
Re: Array dimensions problem from VB6 to .NET
That code doesn't really make sense from a VB.NET perspective. Even if you could do what you want inside the method, you still have the issue of passing in an argument that might be 1D or 2D and handling a return value that might be 1D or 2D. You obviously want the result to be the same type of array as the argument so you should be writing two overloaded methods: one for a 1D array and one for a 2D array.
-
Oct 20th, 2011, 09:16 PM
#3
Thread Starter
Hyperactive Member
Re: Array dimensions problem from VB6 to .NET
Hi jmcilhinney, so in the VB.NET, do we have any array syntax that handling dimension conversion?..like for 2D to 1D or vice versa? I only know there is ArrayCopy syntax for copy array values to another array..
I'm still on the path of learning.... 
-
Oct 20th, 2011, 09:45 PM
#4
Re: Array dimensions problem from VB6 to .NET
There's no such thing as "dimension conversion". Once created, arrays remain the same size and rank as long as they exist. Neither of the ReDim statement and Array.Resize method actually resize an array object, but rather create a new array, optionally copy the elements from the old array and assign the new object to the variable.
-
Oct 20th, 2011, 09:58 PM
#5
Thread Starter
Hyperactive Member
Re: Array dimensions problem from VB6 to .NET
Oic, then i think i do not have much choice for the scenario above. And I have to come out different function which handling different dimension of array.
Anyway, thanks for your advice. I still have a lot of works need to do to port my project from VB6 to .NET.
I'm still on the path of learning.... 
-
Oct 20th, 2011, 10:16 PM
#6
Re: Array dimensions problem from VB6 to .NET
All arrays are one dimensional. You might consider that when you consider how to work with this. I have a project where I need a 2D array of objects, but in many situations, the only valid way to work with this array is as a 1D array of sequential items. There will be 64 rows of 64 columns in the concept that the array is describing. Therefore, I made a 1D array of 4096 elements. Elements 0-63 are the first row, 64-127 are the second row, and so forth. To find any item using (x,y) coordinates, I need only take (64*y)+x. In this way, I can work with the 1D array as if it were a 2D array without needing a 2D array, and I can work with it as a 1D array because it is one. You might consider such a solution, though I suspect it may not be the best in your case.
My usual boring signature: Nothing
 
-
Oct 20th, 2011, 10:46 PM
#7
Thread Starter
Hyperactive Member
Re: Array dimensions problem from VB6 to .NET
Hi Shaggy Hiker, your solution is considerable. But I don't think it is good for my case, as if I need to take your solution, I have to re-write most of the code which is dealing the array. So I think the best way would be come out another function which is dealing with multiple dimension. This will save me a lot of work.
I might consider solution next time when i develop a new project.
Anyway, thanks for your advice.
I'm still on the path of learning.... 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|