Last post Nov 07, 2012 10:37 AM by sotoledu
Apr 03, 2010 11:22 PM|Sam42|LINK
I am working in VB.net. I need to create an array of points. Each point has an x and y coordinate.
I would like to be able to add points to the array dynamically. I have tried using redim preserve to increase the array length, but this seems sloppy and I'm having a hard time trying to make it work. Is there a better way to dynamically add to an array?
Also, I am unsure of weather I should be creating an array of arrays, or a 2 dimensional array.
Apr 04, 2010 12:02 AM|SatyaV|LINK
The redimensioning is taken care of by the container itself if you use an ArrayList or a generic List.
Apr 04, 2010 11:55 AM|Sam42|LINK
Ok, I am trying to use arraylist now. In the code below I add several points to an arraylist. I then try to display them. It seems that I have somehow managed to add three (10,40) points instead of the three different points I tried to add. What am I doing
Dim points As New ArrayList()
Dim pt(1) As Integer
pt(0) = 10
pt(1) = 20
pt(0) = 10
pt(1) = 30
pt(0) = 10
pt(1) = 40
For p = 0 To points.Count - 1
Response.Write("enteredPoints" & points(p)(0) & "," & points(p)(1))
Apr 04, 2010 03:40 PM|Sam42|LINK
I think I know whats going on now. When a cell in the arraylist is assigned a variable, it is not like giving it the value, rather it is now linked to the variable. So if the variable changes, so does the arraylist! So far the only way around this I figure
is to redim the variable that was assigned to the arraylist cell.
Is there a better way?
I am wondering if arraylists are more performance costly to use for this type of thing than just simply arrays. In this case I would prefer arrays, but only if I could add my points dynamically as described in my original post. Any thoughts?
Apr 04, 2010 05:22 PM|Abdulla.AbdelHaq|LINK
What about creating a struct, and then create a generic collection from that struct !
Public Sturct MyPoints
Private X As Integer
Private Y As Integer
public sub new(_x as integer, _y as integer)
X = _x
Y = _y
Dim arr As New List(of MyPoints)
Apr 04, 2010 11:12 PM|Sam42|LINK
got it working thanks!
Nov 07, 2012 10:37 AM|sotoledu|LINK
after that code, how to show the value of arr?