user3029051
user3029051

Reputation: 71

Can I Create an Array of Points Arrays in Visual Basic

Dim myPointsArray As New List(Of Point) creates an array that I can fill with a For Next loop but I have several separate arrays that I want to fill so it would be great if Dim myPointsArrays(20) as.... would work so that I don't have to Dim a points array for each of my separate arrays. Then I could fill them with a nested For Next loop. This is me filling one array

For i = 1 To 6
myPointsArray.Add(New Point(Ox + HxPnt(4, i) / skale, Oy - HyPnt(4, i) / skale))
Next

Here Ox,Oy represent an origin, HxHyPnts are members of an array of points, skale is used to scale the global values to drawing with pixels. Problem is that I need to draw lots of different polylines and polygons from dozens of arrays.

Upvotes: 0

Views: 5228

Answers (2)

user3029051
user3029051

Reputation: 71

Got it! I created a new points array variable (newPoints), filled it from the list then converted it with the .ToArray thingy. Then drew each polyline in the loop by filling, clearing and refilling the array. Is this the best way to do this. Seems good to me.

For Each list As List(Of Point) In myPointsArray
   For Each p As Point In list
      newPoints.Add(New Point((Ox + p.X) / skale, (Oy + p.Y) / skale))
   Next
e.Graphics.DrawLines(myPen, newPoints.ToArray)
newPoints.Clear()
Next

Upvotes: 0

Kenneth
Kenneth

Reputation: 28737

You can create nested lists (List(Of T) is not an Array):

Dim myPointsArray As New List(Of List(Of Point))
For i = 1 To 6
    Dim innerList = new List(Of Point)
    myPointsArray.Add(innerList)
    For j = 1 to 10
        innerList.Add(New Point(Ox + HxPnt(4, i) / skale, Oy - HyPnt(4, i) / skale))
    Next
Next

To iterate again over these values use the following:

For Each list As List(Of Point) in myPointsArray
    For Each p As Point in list
        // Access p here
    Next 
Next

Upvotes: 2

Related Questions