0

我想访问一个包含点的数组列表。下面的示例适用于选项严格关闭。但是我怎样才能在选项严格的情况下以正确的方式做到这一点?提前谢谢了!

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
4

2 回答 2

1

您可能会考虑使用更类型安全的泛型集合(例如List(Of T)代替ArrayList):

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
于 2020-03-13T13:56:31.613 回答
-1

所以这就是解决方案:

        Dim p As Point = CType(ArrayList(0), Point)
        Dim x As Integer = p.X
        Dim y As Integer = p.Y
于 2020-03-13T09:49:54.397 回答