2-dimentinal dynamic array

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 47
Reputation: Nada_ward is an unknown quantity at this point 
Solved Threads: 0
Nada_ward Nada_ward is offline Offline
Light Poster

2-dimentinal dynamic array

 
0
  #1
Aug 2nd, 2009
How I can declare 2 -dimentional resizable array ..
can I use array list as 2- dimensinal ??
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2-dimentinal dynamic array

 
0
  #2
Aug 2nd, 2009
can I use array list as 2- dimensinal ??
Yes you can
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: Nada_ward is an unknown quantity at this point 
Solved Threads: 0
Nada_ward Nada_ward is offline Offline
Light Poster

Re: 2-dimentinal dynamic array

 
0
  #3
Aug 3rd, 2009
OK..
can I use 2-dimensinal array and resize it when I need ??
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: 2-dimentinal dynamic array

 
0
  #4
Aug 3rd, 2009
Use redim statement.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2-dimentinal dynamic array

 
0
  #5
Aug 3rd, 2009
Originally Posted by Nada_ward
can I use 2-dimensinal array and resize it when I need ??
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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: Nada_ward is an unknown quantity at this point 
Solved Threads: 0
Nada_ward Nada_ward is offline Offline
Light Poster

Re: 2-dimentinal dynamic array

 
0
  #6
Aug 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2-dimentinal dynamic array

 
0
  #7
Aug 3rd, 2009
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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: Nada_ward is an unknown quantity at this point 
Solved Threads: 0
Nada_ward Nada_ward is offline Offline
Light Poster

Re: 2-dimentinal dynamic array

 
0
  #8
Aug 3rd, 2009
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 ..
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: 2-dimentinal dynamic array

 
0
  #9
Aug 4th, 2009
the text of label is not appearing completly
Nothing to do with arrays.

I slightly modified the code
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 47
Reputation: Nada_ward is an unknown quantity at this point 
Solved Threads: 0
Nada_ward Nada_ward is offline Offline
Light Poster

Re: 2-dimentinal dynamic array

 
0
  #10
Aug 4th, 2009
Ok , thanks
I will try to solve it ..
Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC