4

Is it possible to have a dictionary of object/classes in vbscript?

For example:

Class employeeclass
    Public first, last, salary
End Class
Dim employeedict: Set employeedict = CreateObject("Scripting.Dictionary")

'Loop would be here
Dim employee: Set employee = new employeeclass
With employee
    .first = "John"
    .last = "Doe"
    .salary = 50000
End With
employeedict.Add "1", employee

EDIT The above does work.

2
  • 3
    In general, "does not work" is not a decent decription of a failure. In this case it is a blatant lie, the code as given stores the 'Doe' object in the dictionary under key "1". Commented Nov 24, 2012 at 19:38
  • It didn't compile before...but it does now...not enough coffee before=P. So apparently it does work (my mistake). I've posted a "community wiki" answer below. Commented Nov 25, 2012 at 0:01

1 Answer 1

5

(Answering own question)

Yes, its possible to use a dictionary of objects/classes in VBScript. The following is an example for those interested:

Class employeeclass
    Public first, last, salary
End Class
Dim employeedict: Set employeedict = CreateObject("Scripting.Dictionary")

Dim employee: Set employee = new employeeclass
With employee
    .first = "John"
    .last = "Doe"
    .salary = 50000
End With
employeedict.Add "1", employee

Set employee = new employeeclass
With employee
    .first = "Mary"
    .last = "Jane"
    .salary = 50000
End With
employeedict.Add "3", employee

Dim employeedetails: Set employeedetails = employeedict.Item("1")
WScript.StdOut.WriteLine("Name:" & employeedetails.first & " " & employeedetails.last & " $" & employeedetails.salary )
WScript.StdOut.WriteLine(employeedict.Item("3").first & " " & employeedict.Item("3").last & " makes $" & employeedict.Item("3").salary)

Prints out:

Name:John Doe $50000 
Mary Jane makes $50000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.