0

Lets suppose i have a class like so:

Public Class Car

Private _id As Integer

Public Sub New()

End Sub

Public Property ID As Integer
    Get
        Return _id
    End Get
    Set(ByVal value As Integer)
        _id = value
    End Set
End Property
End Class

and i have a button that does the following:

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim Stuff As New Car
    'Other code..

End Sub

and i press the button like 10 times...

How can i modify an specific instance of this class (like, the one created when i clicked the button for the third time) to modify its properties?

1
  • Are you doing something with that Car object each time? Or else it's just local to that handler func. Commented Sep 26, 2013 at 17:28

1 Answer 1

2

Your Stuff instance only exists inside the Button click event. To give it larger/longer Scope you need to declare it elsewhere:

Dim Stuff As Car            ' what it is

Private Sub Button2_Click(...
  Stuff = New Car     ' actual instancing/creation of Stuff of Type Car
  'Other code..

End Sub

To make multiples for every click, you need some sort of collection to store them

Dim Cars As New List(Of Car)         ' many other collections will work
Dim CarItem As Car                   ' car instances to put in it

Private Sub Button2_Click(... 
  CarItem = New Car     ' instance of Type Car

  Cars.Add(CarItem)

End Sub

Now, to do something to the third car you made, reference Cars(3):

Cars(3).Make = "Kia"
Cars(3).Color = "Blue"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.