如何声明非标准数组属于类?

时间:2016-02-03 10:41:38

标签: arrays vba class

我有一个VBA课程:

Option Explicit

Public Re As Double 'Real
Public Im As Double 'Imaginary


Public Function CZ_Sqt(Z As Complex, Exp As Integer) As Variant  
   Dim Table() As Complex
   Dim i As Integer

   If Exp > 0 Then
       ReDim Preserve Table(0 To Exp - 1)
       Set Table(UBound(Table)) = New Complex

   Else: Exit Function
   End If

   For i = 0 To UBound(Table)
      Table(i).Re = 1
      Table(i).Im = 1
   Next i
   set CZ_Sqt = Table 

End Function

在模块中:

Sub asd()
    Dim K As Complex
    Dim A As Variant

    Set K = New Complex


    K.Re = 1
    K.Im = 3
    Set A = K.CZ_Sqt(Z, 5)
end sub
  1. 如何在步骤中“设置”所有变量“表”? 在示例中,仅设置元素表(4)但省略其他元素。
  2. 如何将此变量“Table”返回到函数名“CZ_Sqt”? 我提出的这个不起作用。
  3. 如何将变量“C”的变量“CZ_Sqt”传递给变量“A”?

1 个答案:

答案 0 :(得分:0)

您正在使用与对象和对象集合相同的类。

我会将功能分为两类:

  • 复杂
  • ComplexCollection - 包含复杂类
  • 的集合

编辑:ComplexCollection.Add中没有重复检查,并且检查ComplexCollection.Retrieve。

复合

Option Explicit

Public Re As Double
Public Im As Double

ComplexCollection

Option Explicit

Dim oCol As Collection

Public Function Create(pRe As Double, pIm As Double) As Complex
    Dim oResult As Complex
    Set oResult = New Complex
    oResult.Re = pRe
    oResult.Im = pIm

    Set Create = oResult
End Function

Public Sub Add(pComplex As Complex, pKey As String)
     oCol.Add pComplex, pKey
End Sub

Public Function Retrieve(pKey As String) As Complex
     Set Retrieve = oCol(pKey)
End Function

Private Sub Class_Initialize()
    Set oCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set oCol = Nothing
End Sub

Test.bas

Public Sub TestCollection()
    Dim oCL As ComplexCollection
    Dim oC As Complex

    Set oCL = New ComplexCollection

    Set oC = oCL.Create(1, 2)
    Debug.Print oC.Im, oC.Re

    oCL.Add oC, "1"

    Set oC = Nothing
    Set oC = oCL.Retrieve("1")

    Debug.Print oC.Im, oC.Re

End Sub