gpt4 book ai didi

excel - 在类模块内创建公共(public) VB 数组

转载 作者:行者123 更新时间:2023-12-03 00:12:47 27 4
gpt4 key购买 nike

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

更多:

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

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

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

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

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

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

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

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

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

最佳答案

解决方法是在初始化它们时将它们声明为 VariantReDim 为字符串数组

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 并使用“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

关于excel - 在类模块内创建公共(public) VB 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19882691/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com