3

I am having a problem resolving the oList object with my CreateObject("System.Collections.ArrayList")

The error I get in vbscript

"Microsoft VBScript runtime error: Object doesn't support this property or method: 'list.Add'"

Based on this tutorial I know you can use COM wrapped .Net components in vbscript; so why won't this work?

Additional information:

When I am debugging in VS08 and add a watch to list, it says Children could not be evaluated.

The watch for objNode.value has a two character string value. (Which is expected behavior)

Function ProcessXML(oXML) 
  STOP
  xPathExemptions= "//Exemption/@ExemptCodeWord"
  Dim xmlDoc : Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
  xmlDoc.Async = False
  xmlDoc.loadXML(oXML)
 
  Dim colNodes 
  Set colNodes = xmlDoc.selectNodes(xPathExemptions)
  Dim oList
  Set oList = CreateObject("System.Collections.ArrayList")
  Dim objNode
  
  For Each objNode in colNodes
    oList.Add = objNode.value
  Next
  
  'ProcessExemptions = CStr(xmlDoc.selectNodes(xPathExemptions))

End Function 

If you have any comments on my vbscript; please let me know - just started learning and don't know best practices.

1 Answer 1

3

Change:

oList.Add = objNode.value

...to:

oList.Add objNode.value

or (thanks to the guidance from @Ansgar)

Call oList.Add(objNode.value)

Here's a demonstration:

Option Explicit

Dim oList : Set oList = CreateObject("System.Collections.ArrayList")

oList.Add "Banana"
oList.Add "Apple"
oList.Add "Orange"
oList.Add "Grapes"
oList.Add "Plum"

oList.Sort

Dim oItem
For Each oItem In oList
    WScript.Echo oItem
Next

Expected Output:

Apple
Banana
Grapes
Orange
Plum

You can find more on the quirky rules of the use of parentheses with VB and VBScript in Eric Lippert's informative article.

Sign up to request clarification or add additional context in comments.

5 Comments

I will try this at work tomorrow and will give you the points upon success. How odd that you call a function without () operators and you do an assignment without a = . Vbscript is a... different.. Language
xphill64x - Actually, a VBScript Function (which returns a value) does require that its arguments be surrounded by parentheses. Oddly, though, with a Sub (which does not return a value), the parentheses are omitted. Please see my answer here for guidance on downloading a comprehensive VBScript reference as a Windows help file. Finally, note that oList.Add is a Sub with a single argument. And that = serves a dual role as both the assignment operator and for testing for equality.
@DavidRR No. Whether or not parentheses must be used depends on how the procedure or function is called. If a function is called without evaluating the return value (e.g. MyFunction 23, 42) parentheses must not be used. OTOH, if a procedure is called using the Call statement (e.g. Call MySub(23, 42)) parentheses must be used. For more information see Eric Lippert's article on the use of parentheses in VBScript.
@Ansgar - Thank you for the pointer to Eric's informative article. More discussion of the use of parentheses and VBScript in this answer.
@DavidRR I accepted your answer; I have not tested the code.I had to go into a different direction - the client machines may not have .NET installed so I had to go and use a custom array.vbs instead. Once we drop XP support; this will come in handy though! Thanks for the information!

Your Answer

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