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.

Community
  • 1
  • 1

1 Answers1

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.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • 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 –  Dec 15 '13 at 22:23
  • **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](http://stackoverflow.com/a/11371416/1497596) 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 Dec 15 '13 at 23:56
  • 1
    @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](http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx) on the use of parentheses in VBScript. – Ansgar Wiechers Dec 16 '13 at 12:18
  • @Ansgar - Thank you for the pointer to Eric's informative article. More discussion of the use of parentheses and VBScript in [this answer](http://stackoverflow.com/a/1538179/1497596). – DavidRR Dec 16 '13 at 13:46
  • @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! –  Dec 16 '13 at 20:28