问题描述
我在类模块中使用Let属性的理解是,你在类模块中设置它:
My understanding of using the Let property in a class module so far is that you set it up in the class modules like this:
Dim pName as String
Public Property Let Name(Value As String)
pName = Value
End Property
然后你创建了这个类的对象之后,你可以这样设置这个属性:
And then you after you've created an object of this class you can set this property like so:
MyObject.Name = "Larry"
问题:有可能以某种方式在类属性中输入多个参数吗?例如:
Question: Is it possible to somehow enter multiple arguments into a class property? For instance:
Dim pFirstName as String, pLastName as String
Public Property Let Name(FirstName As String, LastName As String)
pFirstName = FirstName
pLastName = LastName
End Property
然后你会如何设置此属性在类外面?
How would you then go about setting this property outside the class?
MyObject.Name = ??
或者这只是平常不可能做的吗?
Or is this just plain not possible to do?
推荐答案
根据你的评论,如果你想封装这个逻辑,那么你可以使用类似下面的东西。
As per your comment if you would prefer to encapsulate this logic then you can use something similar to the below.
下面包含子和函数。该函数返回一个Person对象:
Below includes the sub and function. The function returns a Person object:
Public Sub testing()
Dim t As Person
Set t = PersonData("John", "Smith")
End Sub
Public Function PersonData(firstName As String, lastName As String) As Person
Dim p As New Person
p.firstName = firstName
p.lastName = lastName
Set PersonData = p
End Function
Person类别:
Dim pFirstName As String, pLastName As String
Public Property Let FirstName(FirstName As String)
pFirstName = FirstName
End Property
Public Property Get FirstName() As String
FirstName = pFirstName
End Property
Public Property Let LastName(LastName As String)
pLastName = LastName
End Property
Public Property Get LastName() As String
LastName = pLastName
End Property
这篇关于让VBA类模块的属性 - 有可能有多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!