Results 1 to 7 of 7

Thread: Array dimensions problem from VB6 to .NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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....

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    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
  •  



Click Here to Expand Forum to Full Width