在类模块中创build公共的VB数组

快速总结 – 我正在使用一些遗留代码,我需要添加一个类与string数组,并由于无法声明公共string数组而绊倒。

更多:

我有一个Excel表格,有不同的个人200行数据的VB代码。 每个人的数据将被代码重新混合,并分成一个Word模板,以产生一个个人的报告。 我需要添加另一个代码如下:

我创build了一个类typesMotivationBuckP,我想创build包含可变长度的string数组的类的四个对象。 (如果这更容易,我可能有一种方法可以用固定的长度对它进行编码)

数组开始为空,并根据特定个人的数据填充。 我使用的是数组,因为虽然有固定数量的string内容(18个标题和18个较长的文本),但是每个个体将在四个对象中分布不同(将每个个体想象成一个18加仑的桶四桶)

我明白,在类模块中,我需要将所有variables声明为Public,例如:

Public MotivID As Integer Public MotivQuant As Integer Public strMotivatorTitle() As String Public strMotivatorDescriptor() As String 

但是,为了响应最后2个variables的存在,运行给了我错误:

 Compile error: Constants, fixed-level strings, arrays, user-defined types and Declare statements are not allowed as Public members of object modules 

由于现有代码的限制,我需要在多个模块中创build和使用strMotivatorTitle和strMotivatorDescriptorvariables。 但是我明白我不能这样做,除非它是公开宣布的(而且我假设在课堂模块中)。 所以这就是我撞墙的地方

任何帮助非常感谢。 自从我的博士学位以来,我一直没有使用过VB,所以我可能被一些显而易见的东西绊倒了。

解决方法是在初始化它们时将它们声明为VariantReDim作为string数组

 Public strMotivatorTitle As Variant Public strMotivatorDescriptor As Variant 

例如,初始化为数组

 Private Sub Class_Initialize() ReDim strMotivatorTitle(0 To 9) strMotivatorTitle(0) = "Foo" strMotivatorTitle(1) = "Bar" ' etc End Sub 

另一种方法是,将你的string数组声明为Private并使用`Property Get'来访问它们

 Private pMotivatorTitle() As String Private pMotivatorDescriptor() As String Property Get strMotivatorTitle() As String() strMotivatorTitle = pMotivatorTitle End Property Property Get strMotivatorDescriptor() As String() strMotivatorDescriptor= pMotivatorDescriptor End Property