Loc变量带有下划线,表示无法编入索引,因为它没有默认属性。我可以设置一个默认属性,还是我可以而且应该做的其他事情?
Private Sub tmrLeft_Tick(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tmrLeft.Tick
Dim MoveSpeed As Integer = 10
Dim Loc As Point
If Not picPlayer.Location.X - MoveSpeed < 0 Then
Loc = New Point(picPlayer.Location.X - MoveSpeed, picPlayer.Location.Y)
picPlayer.Location = Loc()
End If
End Sub
答案 0 :(得分:0)
问题是你有一些不必要的括号。您需要更改此行:
picPlayer.Location = Loc()
到此:
picPlayer.Location = Loc
圆括号在VB中可能有点混乱,因为它们可用于方法调用或列表项索引。在此上下文中,Loc
是变量,而不是方法,因此当您将括号放在其后,编译器会假定您必须尝试按索引访问列表的特定项。但是,由于Loc
不是数组,并且它不是具有默认索引可访问属性的列表(如List.Item
,它是默认索引器,因此myList.Item(3)
表示与myList(3)
)。因此,它给出了一个错误,指出Loc
无法编入索引。
答案 1 :(得分:0)
在编程中(在这种情况下为VBA),引用方法时会包括括号:
这是变量
Dim Loc As Point
Dim NewPoint = Loc
这是一种方法
'Function to Add Two Numbers and Then Subtract a Third Number Function SumMinus(dNum1 As Double, dNum2 As Double, dNum3 As Double) As Double SumMinus = dNum1 + dNum2 - dNum3 End Function
Dim Result As Double
Result = SumMinus(..., ..., ...);
如您所见,括号赋予它不同的含义。
variable
method()
Private Sub tmrLeft_Tick(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tmrLeft.Tick
Dim MoveSpeed As Integer = 10
Dim Loc As Point
If Not picPlayer.Location.X - MoveSpeed < 0 Then
Loc = New Point(picPlayer.Location.X - MoveSpeed, picPlayer.Location.Y)
picPlayer.Location = Loc // Without parenthesis
End If
End Sub