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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19882691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:21:44  来源:igfitidea点击:

Creating VB array that is public, within class module

excelvba

提问by user2974801

Quick summary - Im working with some legacy code where I need to add a class with string arrays and tripped up by the inability to declare a Public string array.

快速总结 - 我正在处理一些遗留代码,我需要添加一个带有字符串数组的类,但由于无法声明公共字符串数组而陷入困境。

More:

更多的:

I have VB code with an excel sheet that has 200 lines of data on different individuals. Each individual's data will be remixed by the code and shunted into a Word template to produce a report on the individual. I need to add another piece of code as below:

我有一个带有 200 行不同个人数据的 Excel 表格的 VB 代码。每个人的数据将通过代码重新混合并分流到 Word 模板中以生成关于个人的报告。我需要添加另一段代码,如下所示:

I have created a class type MotivationBuckP and I want to create four objects of that class that contain string arrays of variable length. (there may be a way for me to code it with fixed length if this is easier)

我创建了一个类类型 MotivationBuckP,我想创建该类的四个对象,其中包含可变长度的字符串数组。(如果这更容易的话,我可能有办法用固定长度对其进行编码)

The arrays begin empty and will be filled depending on the particular individual's data. I'm using arrays because although there is a fixed amount of string content (18 titles and 18 longer bits of text), each individual will have them distributed differently across the four objects (think of each individual as a 18-gallon barrel poured into four buckets)

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

I understand that in the Class Module I need to declare all the variables as Public, eg:

我知道在类模块中我需要将所有变量声明为公共,例如:

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

But in response to the presence of the last 2 variables, running gives me the error:

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

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

Due to the constraints of existing code I need to create and use the strMotivatorTitle and strMotivatorDescriptor variables in multiple modules. But I understand I can't do this unless it is declared publically (and, I presume, in the class module). So this is where I hit a wall.

由于现有代码的限制,我需要在多个模块中创建和使用 strMotivatorTitle 和 strMotivatorDescriptor 变量。但我知道我不能这样做,除非它被公开声明(并且,我认为,在类模块中)。所以这就是我撞墙的地方。

Any help much appreciated. I haven't used VB heavily since my PhD so I'm probably being tripped up by something obvious...

非常感谢任何帮助。自从我获得博士学位以来,我就没有大量使用 VB,所以我可能被一些明显的东西绊倒了......

回答by chris neilsen

A work around is to declare them as Variantand ReDimas string array when you initialiose them

一个解决办法是把他们定义为Variant,并ReDim作为字符串数组,当你initialiose他们

Public strMotivatorTitle As Variant 
Public strMotivatorDescriptor As Variant

Initialise as array, for example

初始化为数组,例如

Private Sub Class_Initialize()
    ReDim strMotivatorTitle(0 To 9)

    strMotivatorTitle(0) = "Foo"
    strMotivatorTitle(1) = "Bar"
    ' etc
End Sub

Another way, declare your string arrays as Privateand use `Property Get' to access them

另一种方式,将您的字符串数组声明为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