Initialize the dimensions of a multidimensional array according to parameters in VB.net

jacky Perpète 146 Reputation points
2021-05-25T04:03:08.587+00:00

Hello,

In a VB.Net application I have the following:

  • A one-dimensional array containing values.
  • A variable containing the number of rows.
  • A variable containing the index of each row.
  • A variable representing the type of values (uint, string ...).

From these elements, I need to build a multidimensional array corresponding to the number of rows with their index and distribute the values of the one-dimensional array into my new multidimensional array.

My problem is to define my multidimensional array variable from the number of rows, indexes and type of values contained in the variables.

Dim MyArray (index1, Index2,…,….) As Type (uint, string ...)

How can we achieve this functionality?
I need to create an array, not a list.

Thank you.

Developer technologies | VB
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-05-27T06:07:52.757+00:00

    Hi @jacky Perpète ,
    For value types, you can use Buffer.BlockCopy() method to copy arrays, but for reference types, you need to assign multidimensional array manually.
    An example of two-dimensional array.

            Dim oneDimensionalArray() As ...  
            Dim index() As Integer = {2, 3}  
      
            Dim multiDimensionalArray As Array  
            Dim arrType As Type = oneDimensionalArray.GetType().GetElementType()  
            multiDimensionalArray = Array.CreateInstance(arrType, index)  
      
            Dim rowIndex, colIndex As Integer  
      
            For i As Integer = 0 To oneDimensionalArray.Length - 1  
                rowIndex = Math.Floor(i / index(1))  
                colIndex = i Mod index(1)  
                multiDimensionalArray(rowIndex, colIndex) = oneDimensionalArray(i)  
            Next  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. jacky Perpète 146 Reputation points
    2021-05-26T03:15:33.037+00:00

    Hello,

    Thank you for your reply.
    It works very well for numbers of type integer, byte, long etc ...
    There is just one problem with the Boolean, String, and DateTime type.

    0 comments No comments

  2. jacky Perpète 146 Reputation points
    2021-05-29T09:18:04.95+00:00

    Hello,
    Thank you for your reply.

    multiDimensionalArray (rowIndex, colIndex) = oneDimensionalArray (i) is not accepted with Option Strict On.
    Error BC30574: on disallows late binding.

    Here is my tested code that avoids this problem.

    Private Function CopyValuetableau2D(Of T)(oneDimensionalArray() As T, TypeTableau As Type, Index() As Integer) As Array
    
            'Copie les valeurs des éléments du tableau simple dimension vers un tableau 2 dimensions
    
            Dim index1D As Integer
            Dim myIndicesArray() As Integer
    
            'Crée le tableau multidimensionnel
            Dim multiDimensionalArray As Array = Array.CreateInstance(TypeTableau, Index)
    
            'Copie les données du tableau simple dimension vers le tableau multidimensionnel
            For nbIndex0 As Integer = 0 To multiDimensionalArray.GetUpperBound(0)
                For nbIndex1 As Integer = 0 To multiDimensionalArray.GetUpperBound(1)
                    myIndicesArray = New Integer() {nbIndex0, nbIndex1}
                    multiDimensionalArray.SetValue(oneDimensionalArray(index1D), myIndicesArray)
                    index1D = index1D + 1
                Next
            Next
    
            Return multiDimensionalArray
    
        End Function
    

    Best regards

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.