当另一个 属性 更新时自动更新的变量
Variable that automatically updates when another property is updated
我敢肯定这是一件非常简单的事情,只是我根本不了解 properties/variables。我希望 class 在输入一些 属性 值后有一个变量 assigned/calculated。这不是作业,我只是在使用一个简化的例子。
Public Class person
Public Property name As String
Public Property address As String
Public phoneNumber As String
Public Sub getPhoneNumber()
phoneNumber = lookLookThemUpInThePhoneBook
End Sub
End Class
创建时我可能不知道此人的姓名和地址,但一旦他们都被分配,我希望计算 phone 号码。
我知道我可以做到
Dim Eddy As New person
Eddy.name = "Ed Wood"
Eddy.address = "123 Sample Road"
Eddy.getPhoneNumber()
Debug.Print(Eddy.phoneNumber)
但这将变得非常麻烦,必须手动 运行 该过程,所以我认为必须有一种方法可以主动分配该值。
我希望能够做到这一点
Eddy.name = "Ed Wood"
Eddy.address = "123 Sample Road"
Debug.Print(Eddy.phoneNumber) ' His phone number is just automatically looked up.
您可以延迟 phone 号码查找,直到 phone 号码 属性 被引用,方法是在 属性 的 setter 中实现该逻辑(如评论中所述)。这是一个例子:
Public ReadOnly Property phoneNumber As String
Get
'Leverage a class level private variable and wrap in an
'If Statement if you only want To look it up once.
Return lookLookThemUpInThePhoneBook()
End Get
End Property
然后当您执行 objYourPerson.phoneNumber
时,它会为您查找。
像这样...
Public Class Person
Public Sub New(strname As String, straddress As String)
name = strname
address = straddress
_phoneNumber = LookThemUpInThePhoneBook(strname, straddress)
End Sub
Private Function LookThemUpInThePhoneBook(pName As String, pAddress As String) As String
Dim myPhone As String = ""
'Do work to look up phone number
Return myPhone
End Function
Private _phoneNumber As String
Public ReadOnly Property phoneNumber As String
Get
Return _phoneNumber
End Get
End Property
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
Private _address As String
Public Property address As String
Get
Return _address
End Get
Set(value As String)
_address = value
End Set
End Property
End Class
然后
Dim Eddy As New Person("Eddy wood", "123 Sample Road")
Debug.Print(Eddy.phoneNumber)
Name和Address属性修改时都可以尝试设置phone数字
Public Class Person
Private _name As String = ""
Private _address As String = ""
Private _phoneNumber As String = "not set yet..."
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
setPhoneNumber()
End Set
End Property
Public Property Address As String
Get
Return _address
End Get
Set(value As String)
_address = value
setPhoneNumber()
End Set
End Property
Public Property PhoneNumber As String
Get
Return _phoneNumber
End Get
Private Set(value As String)
_phoneNumber = value
End Set
End Property
Public Sub setPhoneNumber()
If Name <> "" AndAlso Address <> "" Then PhoneNumber = lookThemUpInThePhoneBook(Name, Address)
End Sub
Private Function lookThemUpInThePhoneBook(name As String, address As String) As String
Return "123456789" ' search by name and address here
End Function
End Class
用法:
Sub Main()
Dim Eddy As New Person()
Eddy.Name = "Ed Wood"
Console.WriteLine(Eddy.PhoneNumber)
Eddy.Address = "123 Sample Road"
Console.WriteLine(Eddy.PhoneNumber)
Console.ReadLine()
End Sub
输出
not set yet...
123456789
我敢肯定这是一件非常简单的事情,只是我根本不了解 properties/variables。我希望 class 在输入一些 属性 值后有一个变量 assigned/calculated。这不是作业,我只是在使用一个简化的例子。
Public Class person
Public Property name As String
Public Property address As String
Public phoneNumber As String
Public Sub getPhoneNumber()
phoneNumber = lookLookThemUpInThePhoneBook
End Sub
End Class
创建时我可能不知道此人的姓名和地址,但一旦他们都被分配,我希望计算 phone 号码。
我知道我可以做到
Dim Eddy As New person
Eddy.name = "Ed Wood"
Eddy.address = "123 Sample Road"
Eddy.getPhoneNumber()
Debug.Print(Eddy.phoneNumber)
但这将变得非常麻烦,必须手动 运行 该过程,所以我认为必须有一种方法可以主动分配该值。
我希望能够做到这一点
Eddy.name = "Ed Wood"
Eddy.address = "123 Sample Road"
Debug.Print(Eddy.phoneNumber) ' His phone number is just automatically looked up.
您可以延迟 phone 号码查找,直到 phone 号码 属性 被引用,方法是在 属性 的 setter 中实现该逻辑(如评论中所述)。这是一个例子:
Public ReadOnly Property phoneNumber As String
Get
'Leverage a class level private variable and wrap in an
'If Statement if you only want To look it up once.
Return lookLookThemUpInThePhoneBook()
End Get
End Property
然后当您执行 objYourPerson.phoneNumber
时,它会为您查找。
像这样...
Public Class Person
Public Sub New(strname As String, straddress As String)
name = strname
address = straddress
_phoneNumber = LookThemUpInThePhoneBook(strname, straddress)
End Sub
Private Function LookThemUpInThePhoneBook(pName As String, pAddress As String) As String
Dim myPhone As String = ""
'Do work to look up phone number
Return myPhone
End Function
Private _phoneNumber As String
Public ReadOnly Property phoneNumber As String
Get
Return _phoneNumber
End Get
End Property
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
Private _address As String
Public Property address As String
Get
Return _address
End Get
Set(value As String)
_address = value
End Set
End Property
End Class
然后
Dim Eddy As New Person("Eddy wood", "123 Sample Road")
Debug.Print(Eddy.phoneNumber)
Name和Address属性修改时都可以尝试设置phone数字
Public Class Person
Private _name As String = ""
Private _address As String = ""
Private _phoneNumber As String = "not set yet..."
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
setPhoneNumber()
End Set
End Property
Public Property Address As String
Get
Return _address
End Get
Set(value As String)
_address = value
setPhoneNumber()
End Set
End Property
Public Property PhoneNumber As String
Get
Return _phoneNumber
End Get
Private Set(value As String)
_phoneNumber = value
End Set
End Property
Public Sub setPhoneNumber()
If Name <> "" AndAlso Address <> "" Then PhoneNumber = lookThemUpInThePhoneBook(Name, Address)
End Sub
Private Function lookThemUpInThePhoneBook(name As String, address As String) As String
Return "123456789" ' search by name and address here
End Function
End Class
用法:
Sub Main()
Dim Eddy As New Person()
Eddy.Name = "Ed Wood"
Console.WriteLine(Eddy.PhoneNumber)
Eddy.Address = "123 Sample Road"
Console.WriteLine(Eddy.PhoneNumber)
Console.ReadLine()
End Sub
输出
not set yet...
123456789