Vb.net “访问”;“发件人”;不假思索
我有一个Vb.net “访问”;“发件人”;不假思索,vb.net,Vb.net,我有一个Main类,其中有一个Tomato对象的填充数组(本例中现在显示的填充)。我想访问一个特定的Tomato并使用LowerPrice子例程,而不传递发送方对象 Public Class Main Dim TomatoArray() As Tomato Private Sub HaveATomatoSale 'This is how I want to do my price reduction TomatoArray(0).LowerPrice(10)
Main
类,其中有一个Tomato
对象的填充数组(本例中现在显示的填充)。我想访问一个特定的Tomato
并使用LowerPrice
子例程,而不传递发送方对象
Public Class Main
Dim TomatoArray() As Tomato
Private Sub HaveATomatoSale
'This is how I want to do my price reduction
TomatoArray(0).LowerPrice(10)
'This is NOT how I want to do my price reduction, i.e. including a sender object
TomatoArray(0).LowerPrice(TomatoArray(0), 10)
End Sub
End Class
我在Tomato
类中还有一个函数,如下所示:
Public Class tomato
Dim tomato_price As Integer = 15
Public Property Price As Integer
Get
Return tomato_price
End Get
Set(value As Integer)
tomato_price = value
End Set
End Property
Public Sub LowerPrice(ByVal Decrease As Integer)
'What should I point to here?
sender.tomato_price -= Decrease
End Sub
End Class
我搜索了SO、MSDN和互联网上的其他地方,想找到这个看似简单的问题的简单答案,但没有结果(可能是因为我没有一个好的关键字!)。我该怎么做?谢谢 您要查找的关键字是
Me
:
Public Sub LowerPrice(ByVal Decrease As Integer)
Me.tomato_price -= Decrease
End Sub
请注意,Me
是可选的,因此以下操作也可以:
Public Sub LowerPrice(ByVal Decrease As Integer)
tomato_price -= Decrease
End Sub
事实上,您可能希望利用自动属性并将代码缩减为:
Public Class tomato
Public Property Price As Integer = 15
Public Sub LowerPrice(ByVal decreaseBy As Integer)
Me.Price -= decreaseBy
End Sub
End Class
LowerPrice
是一种方法,它可以降低调用它的番茄对象的价格。在上面的代码中,TomatoArray(0)
(仅)@puropoix是正确的,但是我如何在不将发送方参数传递给我的子例程的情况下访问该对象呢?TomatoArray(0)
就是该对象。不需要传递它,您正在调用该对象上的方法<代码>番茄价格-=减少就是你所需要的,我以为“我”总是指的是主类,但这很有效,这就是我问题的答案。此外,感谢您对自动属性的解释,这无疑将减少程序中的代码量。非常感谢你!