Vb.net 修改类的特定实例

Vb.net 修改类的特定实例,vb.net,Vb.net,假设我有一个这样的类: 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 我有一个按钮可以执行以下

假设我有一个这样的类:

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
我有一个按钮可以执行以下操作:

   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
我按了10次按钮


如何修改此类的特定实例(例如,在我第三次单击按钮时创建的实例)以修改其属性?

您的
内容
实例仅存在于按钮单击事件中。要使其具有更大/更长的
范围
,您需要在其他地方声明它:

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
要使每次点击次数成倍增加,您需要某种集合来存储它们

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
现在,要对您制造的第三辆车做些什么,参考车(3):


你每次都在用那辆
对象做什么吗?或者它只是处理程序func的本地函数。
Cars(3).Make = "Kia"
Cars(3).Color = "Blue"