在类模块内创建公共的VB数组 [英] Creating VB array that is public, within class module
问题描述
快速摘要-我正在处理一些旧代码,在这些代码中,我需要添加带有字符串数组的类,并且由于无法声明公共字符串数组而陷入困境.
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.
更多:
我有一个VB代码和一个Excel工作表,该工作表中有200行关于不同个人的数据.每个人的数据将通过代码重新混合,然后分流到Word模板中以生成关于该人的报告.我需要添加另一段代码,如下所示:
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:
我创建了一个MotivationBuckP类类型,我想创建该类的四个对象,这些对象包含可变长度的字符串数组. (如果这样更容易,我可能会用固定长度的代码进行编码)
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)
数组开始为空,并将根据特定个人的数据填充.我使用数组是因为虽然有固定数量的字符串内容(18个标题和18个较长的文本位),但每个人在四个对象上的分配方式不同(想像每个人将18加仑的桶倒入四个桶)
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)
我了解在类模块中,我需要将所有变量声明为Public,例如:
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
由于现有代码的限制,我需要在多个模块中创建和使用strMotivatorTitle和strMotivatorDescriptor变量.但我知道除非公开声明(而且我假设是在类模块中),否则我将无法做到这一点.所以这就是我碰壁的地方.
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.
任何帮助,不胜感激.自从获得博士学位以来,我就没有大量使用VB,所以我很可能被明显的事情绊倒了.
Any help much appreciated. I haven't used VB heavily since my PhD so I'm probably being tripped up by something obvious...
推荐答案
一种解决方法是,在初始化它们时将它们声明为Variant
和ReDim
作为字符串数组.
A work around is to declare them as Variant
and ReDim
as string array when you initialiose them
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
另一种方法,将字符串数组声明为Private
并使用属性获取"来访问它们
Another way, declare your string arrays as Private
and use `Property Get' to access them
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
这篇关于在类模块内创建公共的VB数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!