想要使用变量来访问类成员

时间:2017-05-02 18:22:29

标签: excel excel-vba vba

我已经定义了一个类clsPowerAtt

Public customerName As String
Public customerSoc As String


sub test ()
  Dim oPowerEntry As New clsPowerAtt

  dim members(5) as string
  members(0) = customerName
  members(1) =customerSoC
  dim entry as variant

  for each entry in members

    oPowerEntry .entry = "foo"

  next each
end sub

我想循环一个定义类成员的数组,这样我就不需要直接调用了。它不喜欢使用变量指向类成员

oPowerEntry .entry = "foo"

如何告诉vba评估条目

1 个答案:

答案 0 :(得分:0)

使用类构造函数。在此示例中,构造函数称为“init”(用于初始化)

您的私人类属性

Private pinp1 As Double
Private pinp2 As Double
Private pinp3 As Double
Private pinp4 As Double
Private pinp5 As Double

你的构造函数。在这里,您可以将所有五个(或多个)属性作为Public Sub的参数传递。当然,您需要用您的属性和数据类型替换我的属性的名称。这只是一个基本构造函数的例子。

Public Sub init(ByVal inp1 As Variant, ByVal inp2 As Variant, ByVal inp3 As Variant, _
            ByVal inp4 As Variant, ByVal inp5 As Variant)
    '
    ' set the private class variables to your parameters
    pinp1 = CDbl(inp1)
    pinp2 = CDbl(inp2)
    pinp3 = CDbl(inp3)
    pinp4 = CDbl(inp4)
    pinp5 = CDbl(inp5)
End Sub