Reputation: 353
I have a Main
class in which I have a populated array (population now shown in this example) of the Tomato
object. I want to access a specific Tomato
and use the LowerPrice
subroutine without passing a sender object.
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
I also have a function inside the Tomato
class like this:
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
I have searched both SO, MSDN and the rest of the internet for a simple answer to this seemingly simply questions but to no avail (probably due to my lack of a good keyword for this!). How can I do this? Thanks!
Upvotes: 0
Views: 27
Reputation: 172260
The keyword you are looking for is Me
:
Public Sub LowerPrice(ByVal Decrease As Integer)
Me.tomato_price -= Decrease
End Sub
Note that Me
is optional, so the following would work as well:
Public Sub LowerPrice(ByVal Decrease As Integer)
tomato_price -= Decrease
End Sub
In fact, you might want to take advantage of automatic properties and reduce your code to:
Public Class tomato
Public Property Price As Integer = 15
Public Sub LowerPrice(ByVal decreaseBy As Integer)
Me.Price -= decreaseBy
End Sub
End Class
Upvotes: 2