0

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Hias
  • 45
  • 6

2 Answers2

1

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
Bart Hofland
  • 3,700
  • 1
  • 13
  • 22
-1

So thats the solution:

        Dim p As Point = CType(ArrayList(0), Point)
        Dim x As Integer = p.X
        Dim y As Integer = p.Y
Hias
  • 45
  • 6