Zeu*_*eus 5 excel vba dictionary module class
我试图将数据保存在类模块中声明的字典中.我在类模块中使用了字典,因为组的数量和关联的数据点在开始时是未知的.下面的代码编译,但dRATIO.exists模块和类模块中的语句都返回false(但是在第一次传递时,类模块中的debug语句给出了正确的值,之后出现错误),然后Function GetRATIO返回999.有什么建议吗?
'CODE IN A CLASS MODULE CALLED clsIVDATA
Option Explicit
Public dRATIO
Public dIV
'
Sub Init(RATIO As Variant, IV As Variant, KEY As String)
'Dim I As Long
Dim VAL As String
Dim RowKeys
Dim COLKEYS
Set dRATIO = CreateObject("Scripting.Dictionary")
Set dIV = CreateObject("Scripting.Dictionary")
dRATIO.ADD ITEM:=RATIO, KEY:=KEY
dIV.ADD ITEM:=RATIO, KEY:=KEY
Debug.Print dRATIO.Exists("1")
Debug.Print dRATIO.ITEM("1")
End Sub
Function GetRATIO(KEY As String)
If dRATIO.Exists(KEY) Then
GetRATIO = dRATIO(KEY)
Else
GetRATIO = 999 'or raise an error...
End If
End Function
Function NO_VALUES()
NO_VALUES = dRATIO.COUNT
End Function
Function GetIV(KEY As String)
If dIV.Exists(KEY) Then
GetIV = dIV(KEY)
Else
GetIV = 999 'or raise an error...
End If
End Function
'=====================================================
'CODE IN A NORMAL MODULE
Sub tstclass()
Dim RATIO() As Variant
Dim IV() As Variant
Dim I As Integer
Dim dctSKEW As Object
Set dctSKEW = CreateObject("Scripting.Dictionary")
dctSKEW.ADD "APZ4", New clsIVDATA
RATIO = Array(0.879, 0.843, 0.802, 0.756, 0.658)
IV = Array(0.165, 0.156, 0.145, 0.136, 0.125)
For I = 1 To 5
KEY = CStr(I)
dctSKEW("APZ4").Init RATIO(I), IV(I), KEY
Next I
Debug.Print dctSKEW("APZ4").GetRATIO("1")
Debug.Print dctSKEW("APZ4").GetRATIO("2")
Debug.Print dctSKEW("APZ4").NO_VALUES
End Sub
Run Code Online (Sandbox Code Playgroud)
你的主要问题是你正在混合初始化Dictioary对象和加载项目(每次你打电话给clsIVDATA.Init你创建一个新的空字典).
此外,生成的数组Array(...)是基于0的,因此您的Option Base 1循环将出错并产生意外结果.
这是您的代码,重构以解决这些问题和一些其他小问题
班级代码
Option Explicit
'CODE IN A CLASS MODULE CALLED clsIVDATA
Private dRATIO As Object
Private dIV As Object
'
Private Sub Class_Initialize()
Set dRATIO = CreateObject("Scripting.Dictionary")
Set dIV = CreateObject("Scripting.Dictionary")
End Sub
Sub Init(RATIO As Variant, IV As Variant, KEY As String)
dRATIO.Add Item:=RATIO, KEY:=KEY
dIV.Add Item:=RATIO, KEY:=KEY
End Sub
Function GetRATIO(KEY As String)
If dRATIO.Exists(KEY) Then
GetRATIO = dRATIO(KEY)
Else
GetRATIO = 999 'or raise an error...
End If
End Function
Function NO_VALUES()
NO_VALUES = dRATIO.Count
End Function
Function GetIV(KEY As String)
If dIV.Exists(KEY) Then
GetIV = dIV(KEY)
Else
GetIV = 999 'or raise an error...
End If
End Function
Run Code Online (Sandbox Code Playgroud)
模块代码
Option Explicit
'=====================================================
'CODE IN A NORMAL MODULE
Sub tstclass()
Dim RATIO() As Variant, KEY As String
Dim IV() As Variant
Dim I As Long
Dim c As clsIVDATA
Dim dctSKEW As Object
Set dctSKEW = CreateObject("Scripting.Dictionary")
dctSKEW.Add "APZ4", New clsIVDATA
Set c = dctSKEW.Item("APZ4")
RATIO = Array(0.879, 0.843, 0.802, 0.756, 0.658)
IV = Array(0.165, 0.156, 0.145, 0.136, 0.125)
For I = 0 To 4
KEY = CStr(I + 1)
c.Init RATIO(I), IV(I), KEY
Next I
Debug.Print dctSKEW("APZ4").GetRATIO("1")
Debug.Print dctSKEW("APZ4").GetRATIO("2")
Debug.Print dctSKEW("APZ4").NO_VALUES
End Sub
Run Code Online (Sandbox Code Playgroud)