oop类在多个subs中使用而不创建新实例

时间:2015-02-18 16:23:45

标签: vb.net class oop

努力使用oop并传递数据。我想使用我的客户类,包含要在多个类中使用的某些数据。

Public Class customer
   Inherits person

 End Class 



Public Class person
    Inherits RegexValidator

Private name As String
Private lastName As String
Private Email As String
Private Postcode As String
Private Number As String



Public Property Firstname As String
    Get
        Return name
    End Get
    Set(ByVal value As String)
        name = value
    End Set
End Property

Public Property LastN As String
    Get
        Return lastName
    End Get
    Set(ByVal value As String)
        lastName = value
    End Set
End Property
End Class 

Soo in another form" Edit Customer"我用数据填充客户,但现在我想以我的其他形式使用同一个客户"添加约会"我需要这样的东西

Class AddAppointment

将AppCustomer变暗为客户 Appcustomer = EditCustomer.Customer

或类似的东西或更好的东西只是让我的客户类

Appcustomer = customer

谢谢:)

1 个答案:

答案 0 :(得分:1)

您只需将实例传递给另一个表单即可。在AddAppointmentForm中,更改构造函数(Sub New)以获取类的实例并将其存储在类变量中:

Public Class AddAppointmentForm

    'Class level variable in AddAppointmentForm
    Private _customer As Customer

    'Constructor takes Customer object as a parameter
    Public Sub New(appCustomer As Customer)
        _customer = appCustomer
    End Sub

End Class

然后在第一种形式中,您将创建实例并将其传递给AddAppointmentForm:

'In the main form create the customer
Dim appCustomer As New Customer

'When ready to show the AddAppointmentForm, pass the customer to it
Dim apptForm As New AddAppointmentForm(appCustomer)