【问题标题】:How do I create a class property (Get) to be an array?如何创建一个类属性 (Get) 作为数组?
【发布时间】:2020-07-09 01:17:12
【问题描述】:

我想将 vba 中的类的 Get 属性设置为数组。我该怎么做呢。

在类模块中

Dim pdbCGX As Double
Dim pdbCGY As Double
Dim pdbCGZ As Double

Public Property Get TheCGv() As Double
    TheCGv(0) = pdbCGX
    TheCGv(1) = pdbCGY
    TheCGv(2) = pdbCGZ
End Property

'在子类中分配数据

pdbCGX = CDbl(extracteddata1)
pdbCGY = CDbl(extracteddata2)
pdbCGZ = CDbl(extracteddata3)

【问题讨论】:

    标签: arrays vba class


    【解决方案1】:

    我建议这样做

    Option Explicit
    
    Dim pdbCGX As Double
    Dim pdbCGY As Double
    Dim pdbCGZ As Double
    
    
    Public Property Get TheCGv() As Variant
    
    Dim v(0 To 2) As Double
    
        v(0) = pdbCGX
        v(1) = pdbCGY
        v(2) = pdbCGZ
    
        TheCGv = v
    
    End Property
    

    如果您想返回数据类型为 double 的数组,您的代码可能如下所示

    Option Explicit
    
    Dim pdbCGX As Double
    Dim pdbCGY As Double
    Dim pdbCGZ As Double
    
    
    Public Property Get TheCGv() As Double()
    
        Dim v(0 To 2) As Double
    
        v(0) = pdbCGX
        v(1) = pdbCGY
        v(2) = pdbCGZ
    
        TheCGv = v
    
    End Property
    

    【讨论】:

      【解决方案2】:

      更好的解决这个问题的办法是不使用数组,而是在类里面放一个 scripting.Dictionary 并且还设置一个枚举来确保对 scripting.Dictionary 的读写是强类型的。

      下面的代码是一个 Scripting.dictionary 的包装类,我根据您上面的帖子添加了一个 Enum。

      Option Explicit
      ' Strongly typed wrapper for the Scripting.Dictionary
      ' Replace KeyType with the Type for the Key to be used  -> done using pdb 
      ' Replace ValueType with the Type for the Values to be used - done using Double
      
      Public Enum pdb
      
          CGX
          CGY
          CGZ
      
      End Enum
      
      Private Type State
          Host                        As Scripting.Dictionary
      End Type
      
      Private s                       As State
      
      
      Private Sub Class_Initialize()
      
          'Set s.Host = CreateObject("Scripting.Dictionary")
          Set s.Host = New Scripting.Dictionary
      
      End Sub
      
      
      '@Description("Add: Adds a new key/item pair to a Dictionary object").
      Public Sub Add(ByVal Key As pdb, ByVal Value As Double)
          s.Host.Add Key, Value
      End Sub
      
      '@Description("Count: Returns the number of key/item pairs in a Dictionary object.")
      Public Function Count() As Long
          Count = s.Host.Count
      End Function
      
      
      '@Description("CompareMode: Sets or returns the comparison mode for comparing keys in a Dictionary object.")
      Public Property Get CompareMode() As Scripting.CompareMethod
          CompareMode = s.Host.CompareMode
      End Property
      
      Public Property Let CompareMode(ByVal Compare As Scripting.CompareMethod)
          s.Host.CompareMode = Compare
      End Property
      
      
      '@Description("Exists  Returns a Boolean value that indicates whether a specified key exists in the Dictionary object.)
      Public Function Exists(ByVal Key As pdb) As Boolean
          Exists = s.Host.Exists(Key)
      End Function
      
      
      '@Description("Item: Get or returns the value of an item in a Dictionary object.")
      '@DefaultMember
      Public Property Get Item(ByVal Key As pdb)
          Item = s.Host(Key)
      End Property
      
      ' Delete Let or Set depending if Value is a primitive or object type
      Public Property Let Item(ByVal Key As pdb, ByVal Value As Double)
          s.Host(Key) = Value
      End Property
      
      Public Property Set Item(ByVal Key As pdb, ByVal Value As Double)
          Set s.Host(Key) = Value
      End Property
      
      
      '@Description("'Items: Returns an array of all the items in a Dictionary object.")
      Public Function Items() As Variant
          Items = s.Host.Items
      End Function
      
      
      '@Description("Key Sets a new key value for an existing key value in a Dictionary object.")
      Public Sub Key(ByVal OldKey As pdb, ByVal NewKey As pdb)
          s.Host.Key(OldKey) = NewKey
      End Sub
      
      
      '@Description("Keys    Returns an array of all the keys in a Dictionary object.")
      Public Function Keys() As Variant
          Keys = s.Host.Keys()
      End Function
      
      
      '@Description("Remove  Removes one specified key/item pair from the Dictionary object.")
      Public Sub Remove(ByVal Key As pdb)
          s.Host.Remove (Key)
      End Sub
      
      
      '@Description("RemoveAll: Removes all the key/item pairs in the Dictionary object.")
      Public Sub RemoveAll()
          s.Host.RemoveAll
      End Sub
      

      如果确实需要数组,例如将值放回 Excel,则脚本字典中的值可以从 .Items 方法作为数组获取。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 2017-03-25
        • 2012-04-20
        相关资源
        最近更新 更多