I get this error whenever the following code is run.

Public Sub test()
    Dim mg As Array = {{2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}}
    Dim pt As Point = New Point(2, 3)

    If mg(1)(0) = pt.X And mg(1)(1) = pt.Y Then 'Checking to see if mg(1) and pt are equal
        Debug.Print("pt and mg are equal")
    End If
End Sub
有帮助吗?

解决方案 2

Try this, for a 2-dimensional array:

Public Sub test()
    Dim mg(,) As Integer = {{2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}}
    Dim pt As System.Drawing.Point = New System.Drawing.Point(2, 3)

    If mg(1, 0) = pt.X And mg(1, 1) = pt.Y Then 'Checking to see if mg(1) and pt are equal
        Debug.Print("pt and mg are equal")
    End If
End Sub

其他提示

Is there a reason why you're not declaring mg as a Point()? Seems like this would simplify the comparison:

Dim mg As Point() = New Point(){New Point(2, 2), New Point(2, 3), New Point(2, 4), New Point(2, 5), New Point(2, 6)}

If mg(1) = pt Then...

Otherwise, as others have said, your array declaration is wrong. It looks like you can access the values in your mg array with Array.GetValue.

If mg.GetValue(1, 0) = pt.X AndAlso mg.GetValue(1, 1) = pt.Y Then...

What you are showing is declaring an array(,) when what your code is expecting is an array()().

To use your code as-is, bar the declaration, declare the array like this:

Dim mg = {({2, 2}), ({2, 3}), ({2, 4}), ({2, 5}), ({2, 6})}

To modify your code to use the declaration as-is, do it like this:

If mg(1, 0) = pt.X AndAlso mg(1, 1) = pt.Y Then

(I use Option Infer On.)

Ref: How to: Initialize an Array Variable in Visual Basic.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top