Dictionary 如何在VBScript中使用字典中的对象?
我创建了一个简单的类GaugeChart:Dictionary 如何在VBScript中使用字典中的对象?,dictionary,object,vbscript,Dictionary,Object,Vbscript,我创建了一个简单的类GaugeChart: Class GaugeChart Public Essential Public Lifestyle Public Spending Public Average End Class 我希望在字典中使用: Dim items, objGaugeDic, objGaugeChart, objGaugeChartread Set objGaugeDic = CreateObject("Scripting.D
Class GaugeChart
Public Essential
Public Lifestyle
Public Spending
Public Average
End Class
我希望在字典中使用:
Dim items, objGaugeDic, objGaugeChart, objGaugeChartread
Set objGaugeDic = CreateObject("Scripting.Dictionary")
Set objGaugeChart = New GaugeChart ' from GaugeChartClass.asp
objGaugeChart.Essential = intChart1Essential
objGaugeChart.Lifestyle = intChart1Lifestyle
objGaugeChart.Spending = intChart1Spending
objGaugeChart.Average = Chart1Avg
Set objGaugeDic ("Goal1") = objGaugeChart
Response.Write("Essential: " + Cstr(objGaugeChart.Essential) + "<br />")
Set objGaugeChart = New GaugeChart ' from GaugeChartClass.asp
objGaugeChart.Essential = intChart2Essential
objGaugeChart.Lifestyle = intChart2Lifestyle
objGaugeChart.Spending = intChart2Spending
objGaugeChart.Average = Chart2Avg
Set objGaugeDic ("Goal2") = objGaugeChart
' load values
'objGuageDic.Add "Goal1", objGaugeElements
Response.Write("objGaugeDic Keys: " + Cstr(objGaugeDic.Count))
For i = 0 To objGaugeDic.Count -1 'Iterate the array
Response.Write( CType(objGaugeDic.Item(i),objGaugeChart).Essential + "<br />")
Next
Dim项,objGaugeDic,objGaugeChart,objGaugeChartread
设置objGaugeDic=CreateObject(“Scripting.Dictionary”)
从GaugeChartClass.asp设置objGaugeChart=New GaugeChart'
objGaugeChart.Essential=intChart1Essential
objGaugeChart.Lifestyle=intChart1Lifestyle
objGaugeChart.EXPENDING=INTCHART1SUNDING
objGaugeChart.平均值=图表1avg
设置objGaugeDic(“Goal1”)=objGaugeChart
回答。写下(“基本要求:+Cstr(objGaugeChart.Essential)+”
)
从GaugeChartClass.asp设置objGaugeChart=New GaugeChart'
objGaugeChart.Essential=intChart2Essential
objGaugeChart.Lifestyle=intChart2Lifestyle
objGaugeChart.EXPENDING=intChart2Spending
objGaugeChart.Average=图表2avg
设置objGaugeDic(“Goal2”)=objGaugeChart
'负载值
'objGuageDic.添加“Goal1”,objgaugeeelements
响应.写入(“objGaugeDic键:”+Cstr(objGaugeDic.Count))
对于i=0的objGaugeDic.Count-1'迭代数组
响应.写入(CType(objGaugeDic.Item(i),objGaugeChart.Essential+“
”)
下一个
字典键=2-所以这很好。但是,当我尝试使用CType访问字典中的对象和属性时,我会遇到一个运行时错误
尝试使用objGaugeDic.Item(i).Essential
时未定义
我也尝试过,而不是Set objGaugeDic(“Goal1”)=objGaugeChart,去做objGaugeDic.Add“Goal1”,objGaugeChart`
如何访问objGaugeDic
类型的每个字典项的对象属性,例如Response.Write(CType(objGaugeDic.item(I),objGaugeChart).Essential+“
”)
?对于每个循环,您可以通过中的键引用字典中的对象:
objGaugeDic中每个strKey的
回答。写(objGaugeDic(strKey).Essential+“
”)
下一个
此外,您还可以获得循环外部的键数组,并通过索引键引用字典中的对象:
arrKeys=objGaugeDic.Keys()
对于i=0到objGaugeDic.Count-1
响应.写入(objGaugeDic(arrKeys(i)).Essential+“
”)
下一个