vba - 在 VBA 中链接类

标签 vba excel class oop object

我在VBA中定义了一个类如下

员工类(clsEmployee):

Private pEmpName As String
Private pEmpID As Long
Private pDOJ As Date
Private pManager As clsEmployee
Private pOffice As clsOffice

其中 pManager 是 clsEmployee 类的属性,pOffice 也是 clsOffice 类(另一个类)的属性

我还在类中定义了 Let 和 Get 方法来读取和写入类的属性,如下所示

Public Property Let Name(sName As String)
    If sName <> "" Then
        pEmpName = sName
    End If
End Property

Public Property Get Name() As String
    Name = pEmpName
End Property

Public Property Let EmployeeId(lngID As Long)
    If IsNumeric(lngID) Then
        pEmpID = lngID
    End If
End Property

Public Property Get EmployeeId() As Long
    EmployeeId = pEmpID
End Property

Public Property Let PDM(obj As clsEmployee)
    Set pPDManager = obj
End Property

Public Property Get PDM() As clsEmployee
    Set PDM = pPDManager
End Property

现在在代码模块中,我编写了一个test sub 来设置我的Employee 类的一些属性,如下所示

    Sub test()
    Dim obj As clsEmployee

    Set obj = New clsEmployee

    obj.Name = "Employee 100"
    obj.EmployeeId = 11111111
    obj.PDM.Name = "Employee 1"

当代码执行语句 obj.Name="Employee 100" 时,Let 属性即 Name 被执行并设置 pEmpName,而当代码尝试执行 statemnet obj.PDM.Name="Employee 1 VBA 执行 GET 方法 PDM

我的问题是,为什么 get 方法(类中的 PDM)在语句 `obj.PDM.Name="Employee 1"上执行,而显然我正在尝试设置属性而不是检索

最佳答案

where pManager is a property of the class of type clsEmployee

此类 clsEmployee 的每个实例都必须引用另一个 clsEmployee 实例,后者表示经理。员工必须有其经理。

属性 PDM 重新调整 clsEmployee 所以它需要使用 Set

Public Property Set PDM(obj As clsEmployee)
    Set pManager = obj
End Property

Public Property Get PDM() As clsEmployee
    Set PDM = pManager
End Property

why does the get method (PDM in class) gets executed on statement `obj.PDM.Name="Employee 1"

员工有其经理。此管理器可通过属性 PDM 访问。如果我们在变量 obj 中引用了员工并且我们想要更改该员工的经理的姓名,我们必须找到其经理。这就是调用 PDM 的原因。所以我们找到经理并可以更改其名称。

测试方法现在可以修改如下。

Option Explicit

Sub test()
    Dim mgr As clsEmployee
    Set mgr = New clsEmployee

    Dim obj As clsEmployee

    Set obj = New clsEmployee
    Set obj.PDM = mgr

    obj.Name = "Employee 100"
    obj.EmployeeId = 11111111
    obj.PDM.Name = "Employee 1"
End Sub

当我们想要一个设置对对象的引用的属性时,我们需要使用 Set-Property。否则 Let 就足够了。

按照惯例,我们使用 Set 作为对象引用,例如您的员工。 Employee 是 NameIdDateOfBirth 等数据的组合。 Let 用于原子数据,如 stringintegerbool

查看 Excel 库的 Worksheet 类。此类具有 Name 类型的属性 string。如果我们有一个引用特定工作表的变量,那么我们可以像这样更改工作表的名称:

Dim w as Worksheet
Set w = Worksheets(1)
Let w.Name = "Demo1"

注意 Let 在这里。因为 Namestring-Property Let 被使用。不过关键字 Let 可以省略。

Worksheet 具有 Object 类型的属性 Parent,它是对特定工作集父级的引用。如果我们想改变 parent ,我们会写:

Dim w as Worksheet
Set w = Worksheets(1)
Set w.Parent = new Parent ' Is just example, it won't compile, Parent is read-only :)

如果需要 Set,则不能像 Let 那样省略它。


在您的情况下,与 clsEmployee 一起使用的属性必须使用 Get-Set,其中与例如一起使用的属性string 使用 Get-Let

Here in my Dropbox I have created an very ugly picture which should illustrate the situation with the object references in variables obj and mgr.

关于vba - 在 VBA 中链接类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736610/

相关文章:

excel - 需要优化一个慢 vba 代码

excel - 突出显示连续正数的最大数量

vba - excel vba dir函数查找doc而不是docx

excel - 将 IF(使用 ISTEXT、FIND、MID 将日期转换为正确的格式)语句转换为自定义 VBA 函数

arrays - Swift-在不同的类中使用数组

vba - 被宏复制后更改单元格值

arrays - 具有三个条件的索引/匹配

VBA运行时错误: Clear Contents of an Excel sheet

c++ - C++20 是否取消了类成员按升序排列的要求?

python - 如何在类中调用函数?