locked
vb.net dynamic array of points RRS feed

  • Question

  • User-881450349 posted

    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


    Saturday, April 3, 2010 11:22 PM

Answers

  • User-55773023 posted

    The redimensioning is taken care of by the container itself if you use an ArrayList or a generic List.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 12:02 AM
  • User1282917655 posted

    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


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 5:22 PM

All replies

  • User-55773023 posted

    The redimensioning is taken care of by the container itself if you use an ArrayList or a generic List.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 12:02 AM
  • User-881450349 posted

    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



    Sunday, April 4, 2010 11:55 AM
  • User-881450349 posted

    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

    Sunday, April 4, 2010 3:40 PM
  • User1282917655 posted

    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


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2010 5:22 PM
  • User-881450349 posted

    got it working thanks!

    Sam

    Sunday, April 4, 2010 11:12 PM
  • User2084237604 posted

    after that code, how to show the value of arr?

    Wednesday, November 7, 2012 10:37 AM