更新对象字典中的对象属性?

时间:2017-01-26 16:57:47

标签: class object dictionary vbscript

我有一个对象字典。每个对象都是错误的。当我遍历日志中的错误时,我将对象添加到字典中,使用描述作为键。如果密钥已存在,我想更新计数。这是我遇到麻烦的地方;更新计数。

定义:

Class errClass
    Dim errDesc, errType, hasPhr, errCount, lwCount, errPriority, edge
End Class

Dim errs: Set errs = CreateObject("Scripting.Dictionary")

人口:

errDetails.errDesc = resultSet("eventtext")
errDetails.errType = resultSet("eventtype")
errDetails.edge = resultSet("namespace")
errDetails.errPriority = 1

If errs.Exists(errDetails.errDesc) Then
    If dev Then WScript.Echo "Key exists, incrementing count"
    errs.Item(errDetails.errDesc)("errCount") = errs.Item(errDetails.errDesc)("errCount") + 1
Else
    If dev Then WScript.Echo "Key does not exist, adding"
    errs.Add errDetails.errDesc, errDetails
End If

上面是错误发生的地方:errs.Item(errDetails.errDesc)("errCount") = errs.Item(errDetails.errDesc)("errCount") + 1

  

Microsoft VBScript运行时错误:对象不支持此属性或方法:'errs.Item(...)'

更新字典中对象属性的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

Ansgar在评论中这样做了,但这里是代码示例。

Class errClass
    Public errDesc, errType, hasPhr, errCount, lwCount, errPriority, edge
End Class

dim errDetails : set errdetails = new errClass

Dim errs: Set errs = CreateObject("Scripting.Dictionary")
dim dev: dev = true
errDetails.errDesc = "test"
errDetails.errType = "test"
errDetails.edge = "test"
errDetails.errPriority = 1

errs.Add errDetails.errDesc, errDetails

errs.Item(errDetails.errDesc).errCount = errs.Item(errDetails.errDesc).errCount + 100

msgbox errs.Item(errDetails.errDesc).errCount

答案 1 :(得分:0)

正如其他人指出的那样,你不能使用字符串来引用对象属性。

但是当对象是引用时,你的字典需要保存不同的实例,并且计数从1开始。所以:

Class errClass
    Dim errDesc, errCount
    Public Sub show
      WScript.Echo errDesc, errCount
    End Sub
End Class

' dictionary of errClass objects
Dim errs : Set errs = CreateObject("Scripting.Dictionary")

Dim e
For Each e In Split("stupid silly stupid")
    If errs.Exists(e) Then 
       WScript.Echo "Key exists, incrementing count"
       errs(e).errCount = errs(e).errCount + 1
    Else
       WScript.Echo "new Key, adding new object to dictionary"
       Dim o : Set o = New errClass : o.errDesc = e : o.errCount = 1
       errs.add e, o
    End If
Next

For Each e In errs.Keys()
    errs(e).show
Next

输出:

cscript 41878841.vbs
new Key, adding new object to dictionary
new Key, adding new object to dictionary
Key exists, incrementing count
stupid 2
silly 1