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.
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
wrong?
Thanks,
Sam
Dim points As New ArrayList()
Dim pt(1) As Integer
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?
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
end sub
End Struct
Dim arr As New List(of MyPoints)
arr.Add(new MyPoints(2,5))
arr.Add(new MyPoints(10,333))
.... etc
Plz remember to click "Mark as Answer" if this helped you.
Abdulla Abdelhaq .NET/SharePoint Team Leader
My Articles blog
Marked as answer by Sam42 on Apr 05, 2010 03:13 AM
Sam42
Member
78 Points
226 Posts
vb.net dynamic array of points
Apr 04, 2010 03:22 AM|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.
Thanks,
Sam
SatyaV
Participant
875 Points
145 Posts
Re: vb.net dynamic array of points
Apr 04, 2010 04:02 AM|LINK
The redimensioning is taken care of by the container itself if you use an ArrayList or a generic List.
Sam42
Member
78 Points
226 Posts
Re: vb.net dynamic array of points
Apr 04, 2010 03:55 PM|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 wrong?
Thanks,
Sam
Dim points As New ArrayList()
Dim pt(1) As Integer
pt(0) = 10
pt(1) = 20
points.Add(pt)
pt(0) = 10
pt(1) = 30
points.Add(pt)
pt(0) = 10
pt(1) = 40
points.Add(pt)
For p = 0 To points.Count - 1
Response.Write("enteredPoints" & points(p)(0) & "," & points(p)(1))
next
Sam42
Member
78 Points
226 Posts
Re: vb.net dynamic array of points
Apr 04, 2010 07:40 PM|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?
Thanks,
Sam
Abdulla.Abde...
Contributor
5538 Points
871 Posts
Re: vb.net dynamic array of points
Apr 04, 2010 09:22 PM|LINK
What about creating a struct, and then create a generic collection from that struct !
Abdulla Abdelhaq .NET/SharePoint Team Leader
My Articles
blog
Sam42
Member
78 Points
226 Posts
Re: vb.net dynamic array of points
Apr 05, 2010 03:12 AM|LINK
got it working thanks!
Sam
sotoledu
Member
2 Points
1 Post
Re: vb.net dynamic array of points
Nov 07, 2012 02:37 PM|LINK
after that code, how to show the value of arr?