Arraylist access to x and y when a drawing.point is inside

136 views Asked by At

I want to access an arraylist which contains points. The below example works with option strict off. But how can I do this on the correct way with option strict on? Many thanks in advance!

Option Strict Off
Imports System.Drawing

Module Module2
    Sub Main()

        Dim ArrayList As New ArrayList
        Dim R As New Random

        For i = 0 To 9
            ArrayList.Add(New Point(R.Next(50), R.Next(50)))
        Next i

        Dim firstY As Integer = ArrayList(0).Y
        Dim firstX As Integer = ArrayList(0).X

    End Sub
End Module
2

There are 2 answers

1
Hias On

So thats the solution:

        Dim p As Point = CType(ArrayList(0), Point)
        Dim x As Integer = p.X
        Dim y As Integer = p.Y
0
Bart Hofland On

You might consider using generic collections (like List(Of T) instead of ArrayList), which are more type-safe:

Imports System.Collections.Generic
Imports System.Drawing

Module Module2
    Sub Main()

        Dim Points As New List(Of Point)
        Dim R As New Random

        For i = 0 To 9
            Points.Add(New Point(R.Next(50), R.Next(50)))
        Next i

        Dim firstY As Integer = Points(0).Y
        Dim firstX As Integer = Points(0).X

    End Sub
End Module