How I can declare 2 -dimentional resizable array ..
can I use array list as 2- dimensinal ??

Recommended Answers

All 9 Replies

can I use array list as 2- dimensinal ??

Yes you can

Dim MyList As New ArrayList()
Dim Arr1() As Integer = {1, 2, 3}
Dim Arr2() As Integer = {4, 5, 6}

MyList.Add(Arr1)
MyList.Add(Arr2)

MsgBox(CType(MyList.Item(1), Integer())(2)) ' -> Second item (array) and third item in the array, output: 6

Just remember that ArrayList stores items of type Object.

HTH

OK..
can I use 2-dimensinal array and resize it when I need ??

Use redim statement.

can I use 2-dimensinal array and resize it when I need ??

Use redim statement.

ReDim works fine unless you have to resize 2-dimensional array more than once and preserve the previous content. That's a limitation by design.

For example

Dim Matrix(,) As Integer

ReDim Preserve Matrix(0, 0)
Matrix(0, 0) = 1 ' Matrix: (0,0) = 1

ReDim Preserve Matrix(0, 1)
Matrix(0, 1) = 2 ' Matrix: (0,0) = 1 and (0,1) = 2

ReDim Preserve Matrix(1, 1) ' <- This raises error: "'ReDim' can only change the rightmost dimension"
Matrix(1, 1) = 3 ' Code never reaches here

but I want resize the length of array
my app recieve information from user , I don't know how many input will request user
app recieves information step by step
each step the length of information will increase
the rightmost dimention will be fixed

I don't know how many input will request user

That's why you use ReDim Preserve to resize array/matrix and to preserve previously entered data.

the rightmost dimention will be fixed

First dimension has to be fixed. You can only change the second dimension.

Here's another example

Const FIRST_DIM As Integer = 9 ' First dimension gets fixed to ten elements
Dim Matrix(,) As Integer
Dim Counter As Integer ' Counter for the second dimension of the matrix

Counter = 0 ' Reset counter

' A loop to process user input
Do Until <some condition>
  ' Resize second dimension
  ReDim Preserve Matrix(FIRST_DIM, Counter) ' ReDim Preserve preserves previous data in the matrix

  ' Process/add user data here

  Counter += 1 ' Increase counter!
Loop

So, the first dimension stays always fixed. You have to design your app (or the organization of the data) so that the second dimension changes, not the first one. That's something you should always be able to do with two-dimensional arrays. It just may require a bit changing the way you think of organizing the data :)

HTH

Ok
I though on that
but when I do it , the text of label is not appearing completly
I didn't change any thing except replacing the place of dimension for array ..??
see the code ..

Public Class Form1
    Dim counter As Integer=3 'suppose counter=number of columns in 2-dimention 
    Dim g As Graphics
    Dim d As Integer
    Dim s As SizeF
    Dim l1 As Label
    Dim array1() = {"c1", "c2", "c3"}
    Dim frag_des(,) As String = {{"c1", "c2", "c3"}, {"ddddddddddd ", "ffffffffff ", "ggggggggggggggg"}}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For j As Integer = 0 To array1.Length - 1
            d = 0
            l1 = New Label()
            
            
            While Not (array1(j) = frag_des(0, d)) And d < counter
                'frag_des.GetUpperBound(0)
                d += 1
            End While

            If Not (array1(j) = frag_des(0, d)) Then
                g = l1.CreateGraphics
                s = g.MeasureString(array1(j), l1.Font)
                l1.Height = s.Height
                l1.Width = s.Width
                l1.Text = array1(j)
            Else
                g = l1.CreateGraphics
                s = g.MeasureString(array1(j) & ": " & frag_des(1, d), l1.Font)
                l1.Height = s.Height
                l1.Width = s.Width
                l1.Text = array1(j) & ": " & frag_des(1, d)
                MsgBox(array1(j) & ": " & frag_des(1, d))
            End If

            l1.Location = New Point(5, (j * 25) + 3)



            Me.Controls.Add(l1)
        Next
    End Sub
End Class

the text of label is not appearing completly

Nothing to do with arrays.

I slightly modified the code

Else
  g = l1.CreateGraphics
  s = g.MeasureString(array1(j) & ": " & frag_des(1, d), l1.Font)
  g = Nothing
  l1.Height = CInt(s.Height)
  l1.Width = CInt(s.Width) + 5
  l1.Text = array1(j) & ": " & frag_des(1, d)
  'MsgBox(array1(j) & ": " & frag_des(1, d))
End If

i.e. added some "extra" width to the label and disposed previous graphics object. After that all the labels displayed properly. Your original code should have worked but that "ffffff" string wasn't displayed correctly for some reason :-/

Ok , thanks
I will try to solve it ..
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.