Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA Class()对象作为另一个类的属性_Excel_Class_Vba - Fatal编程技术网

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

Excel VBA Class()对象作为另一个类的属性,excel,class,vba,Excel,Class,Vba,我试图创建一个类来保存数量可变的项(它们本身就是另一个类对象) 所以,我有第二课: ' 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 S

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

所以,我有第二课:

' 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 “类别2包含每个单独的报价元素(OTC和MRC) 私有pOTC作为字符串 私有pMRC作为字符串 公共属性Get OTC()作为字符串 OTC=pOTC 端属性 公共属性(值为字符串) pOTC=值 端属性 公共属性获取MRC()作为字符串 MRC=pMRC 端属性 公共属性出租MRC(值为字符串) pMRC=值 端属性 然后类1包含类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 私有pCurr作为字符串 私人pQuote(20)作为2级 公共属性Get Curr()作为字符串 Curr=pCurr 端属性 公共属性Let Curr(值为字符串) pCurr=值 端属性 公共属性集合Quote(索引为整数,cQuote为类2) 设置pQuote(索引)=cQuote 端属性 公共属性获取Quote(索引为整数)作为类2 Quote=pQuote(索引) 端属性 我想做的是:

Dim myQuotes As Class1 Set myQuotes = New Class1 myQuotes.Curr = "GBP" myQuotes.Quote(3).OTC = "1200" 将myQuotes设置为Class1 设置myQuotes=newclass1 myQuotes.Curr=“英镑” myQuotes.Quote(3).OTC=“1200” 第一行设置myQuotes.Curr没有问题,但是当我尝试在数组中设置值时,下一行错误与运行时91对象变量或未设置块变量有关

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

提前谢谢

也许应该是这样

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

您需要将它们设置为Class1中的新Class2:

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

就像您在最后一个脚本中使用Class1一样。

当您调用
myQuotes.Quote(3)
时,调用
Property Get Quote
,它有一个问题

您的
Class2
的内部数组未实例化,因此
pQuote(Index)
引用了
Nothing
的数组元素,当您尝试将
myQuotes.Quote(3).OTC=
分配给
Nothing
时失败

您需要确保
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
(注意所需的
设置

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

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

除了下面感谢Alex K为您的问题提供的解决方案之外,我可以(出于好奇)问一下,您为什么要这样做,而不是使用一组引号?谢谢!成功了!!我还在Class1中发现了另一个错误,因为它应该读为Set Quote=pQuote(Index):
公共属性Get Quote(Index为整数)作为Class2 Set Quote=pQuote(Index)End属性