VBA Class() 对象作为另一个类的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17486215/
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-11 22:04:17  来源:igfitidea点击:

VBA Class() object as property of another class

excelclassvba

提问by freudian

I'm trying to create a class to hold a variable number of items (which are themselves another class object).

我正在尝试创建一个类来保存可变数量的项目(它们本身就是另一个类对象)。

So, I have Class 2:

所以,我有第 2 类:

' Class 2 contain each individual quote elements (OTC and MRC)

Private pOTC As String
Private pMRC As String
Public Property Get OTC() As String
    OTC = pOTC
End Property
Public Property Let OTC(Value As String)
    pOTC = Value
End Property

Public Property Get MRC() As String
    MRC = pMRC
End Property
Public Property Let MRC(Value As String)
    pMRC = Value
End Property

Then Class 1 contains an array of Class 2:

那么 Class 1 包含一个 Class 2 的数组:

Private pCurr As String
Private pQuote(20) As Class2

Public Property Get Curr() As String
    Curr = pCurr
End Property
Public Property Let Curr(Value As String)
    pCurr = Value
End Property

Public Property Set Quote(Index As Integer, cQuote As Class2)
    Set pQuote(Index) = cQuote
End Property

Public Property Get Quote(Index As Integer) As Class2
    Quote = pQuote(Index)
End Property

And what I would like to do is something like:

我想做的是:

Dim myQuotes As Class1
Set myQuotes = New Class1

myQuotes.Curr = "GBP"
myQuotes.Quote(3).OTC = "1200"  

The first line setting myQuotes.Curr is no problem, however when I try to set a value inside the array the next line errors with Run-time 91 Object variable or With block variable not set

第一行设置 myQuotes.Curr 没问题,但是当我尝试在数组中设置一个值时,下一行会出现Run-time 91 Object variable 或 With block variable not set 错误

Any pointers as to what I'm doing wrong and how can I set the values for the elements within the class array?

关于我做错了什么以及如何设置类数组中元素的值的任何指针?

Thanks in advance!

提前致谢!

采纳答案by Alex K.

When you myQuotes.Quote(3)you call Property Get Quotewhich has an issue.

当你myQuotes.Quote(3)打电话给Property Get Quote哪个有问题时。

Your internal array of Class2is not instantiated so pQuote(Index)refers to an array element of Nothing, when you then myQuotes.Quote(3).OTC =you try to assign to Nothingwhich fails.

您的内部数组 ofClass2未实例化,因此pQuote(Index)指的是 的数组元素Nothing,当myQuotes.Quote(3).OTC =您尝试分配Nothing失败时。

You need to make sure pQuote(Index)is instanced; you can do this on demand:

您需要确保pQuote(Index)已实例化;您可以按需执行此操作:

Public Property Get Quote(Index As Integer) As Class2
   If (pQuote(Index) Is Nothing) Then Set pQuote(Index) = New Class2
   Set Quote = pQuote(Index)
End Property

(Note the required Set)

(注意必填Set

Or by adding an intitialisation routine to Class1:

或者通过添加一个初始化例程到Class1

Private Sub Class_Initialize()
    Dim Index As Long
    For Index = 0 To UBound(pQuote)
         Set pQuote(Index) = New Class2
    Next
End Sub

回答by K_B

You need to set them as New Class2 in Class1:

您需要在 Class1 中将它们设置为 New Class2:

For intI = LBOUND(pQuote) to UBOUND(pQuote)
    Set pQuote(intI) =  New Class2
Next IntI

Just as you do with Class1in your final script.

就像您在最终脚本中对Class1所做的一样。

回答by matzone

Maybe it should be

也许应该是

Public Property Let Quote(Index As Integer, cQuote As Class2)
    Set pQuote(Index) = cQuote
End Property