943,682 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2079
  • VB.NET RSS
Aug 2nd, 2009
0

2-dimentinal dynamic array

Expand Post »
How I can declare 2 -dimentional resizable array ..
can I use array list as 2- dimensinal ??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009
Aug 2nd, 2009
0

Re: 2-dimentinal dynamic array

Quote ...
can I use array list as 2- dimensinal ??
Yes you can
VB.NET Syntax (Toggle Plain Text)
  1. Dim MyList As New ArrayList()
  2. Dim Arr1() As Integer = {1, 2, 3}
  3. Dim Arr2() As Integer = {4, 5, 6}
  4.  
  5. MyList.Add(Arr1)
  6. MyList.Add(Arr2)
  7.  
  8. 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

OK..
can I use 2-dimensinal array and resize it when I need ??
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

Use redim statement.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

Quote originally posted by Nada_ward ...
can I use 2-dimensinal array and resize it when I need ??
Quote originally posted by adatapost ...
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
VB.NET Syntax (Toggle Plain Text)
  1. Dim Matrix(,) As Integer
  2.  
  3. ReDim Preserve Matrix(0, 0)
  4. Matrix(0, 0) = 1 ' Matrix: (0,0) = 1
  5.  
  6. ReDim Preserve Matrix(0, 1)
  7. Matrix(0, 1) = 2 ' Matrix: (0,0) = 1 and (0,1) = 2
  8.  
  9. ReDim Preserve Matrix(1, 1) ' <- This raises error: "'ReDim' can only change the rightmost dimension"
  10. Matrix(1, 1) = 3 ' Code never reaches here
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

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

Quote ...
the rightmost dimention will be fixed
First dimension has to be fixed. You can only change the second dimension.

Here's another example
VB.NET Syntax (Toggle Plain Text)
  1. Const FIRST_DIM As Integer = 9 ' First dimension gets fixed to ten elements
  2. Dim Matrix(,) As Integer
  3. Dim Counter As Integer ' Counter for the second dimension of the matrix
  4.  
  5. Counter = 0 ' Reset counter
  6.  
  7. ' A loop to process user input
  8. Do Until <some condition>
  9. ' Resize second dimension
  10. ReDim Preserve Matrix(FIRST_DIM, Counter) ' ReDim Preserve preserves previous data in the matrix
  11.  
  12. ' Process/add user data here
  13.  
  14. Counter += 1 ' Increase counter!
  15. 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: 2-dimentinal dynamic array

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 ..
vb .net Syntax (Toggle Plain Text)
  1. Public Class Form1
  2. Dim counter As Integer=3 'suppose counter=number of columns in 2-dimention
  3. Dim g As Graphics
  4. Dim d As Integer
  5. Dim s As SizeF
  6. Dim l1 As Label
  7. Dim array1() = {"c1", "c2", "c3"}
  8. Dim frag_des(,) As String = {{"c1", "c2", "c3"}, {"ddddddddddd ", "ffffffffff ", "ggggggggggggggg"}}
  9.  
  10. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11. For j As Integer = 0 To array1.Length - 1
  12. d = 0
  13. l1 = New Label()
  14.  
  15.  
  16. While Not (array1(j) = frag_des(0, d)) And d < counter
  17. 'frag_des.GetUpperBound(0)
  18. d += 1
  19. End While
  20.  
  21. If Not (array1(j) = frag_des(0, d)) Then
  22. g = l1.CreateGraphics
  23. s = g.MeasureString(array1(j), l1.Font)
  24. l1.Height = s.Height
  25. l1.Width = s.Width
  26. l1.Text = array1(j)
  27. Else
  28. g = l1.CreateGraphics
  29. s = g.MeasureString(array1(j) & ": " & frag_des(1, d), l1.Font)
  30. l1.Height = s.Height
  31. l1.Width = s.Width
  32. l1.Text = array1(j) & ": " & frag_des(1, d)
  33. MsgBox(array1(j) & ": " & frag_des(1, d))
  34. End If
  35.  
  36. l1.Location = New Point(5, (j * 25) + 3)
  37.  
  38.  
  39.  
  40. Me.Controls.Add(l1)
  41. Next
  42. End Sub
  43. End Class
Last edited by Nada_ward; Aug 3rd, 2009 at 8:51 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009
Aug 4th, 2009
0

Re: 2-dimentinal dynamic array

Quote ...
the text of label is not appearing completly
Nothing to do with arrays.

I slightly modified the code
VB.NET Syntax (Toggle Plain Text)
  1. Else
  2. g = l1.CreateGraphics
  3. s = g.MeasureString(array1(j) & ": " & frag_des(1, d), l1.Font)
  4. g = Nothing
  5. l1.Height = CInt(s.Height)
  6. l1.Width = CInt(s.Width) + 5
  7. l1.Text = array1(j) & ": " & frag_des(1, d)
  8. 'MsgBox(array1(j) & ": " & frag_des(1, d))
  9. 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 4th, 2009
0

Re: 2-dimentinal dynamic array

Ok , thanks
I will try to solve it ..
Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: DateTime stamp or Code?
Next Thread in VB.NET Forum Timeline: the text of dynamic label is not complete





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC