mst1
11 years agoOccasional Contributor
For Each not working with dotNET.system_collections.ArrayList in vbScript
Hello,
I want to use an ArrayList object from the system.collections library, because of it's great member functionallities.
However, when I create such an object as next , I am not able to use For Each loops:
Dim myList
Set myList = dotNET.System_Collections.ArrayList.zctor()
myList.Add "banana"
myList.Add "apple"
For Each element In myList
msgbox element
Next
However, when I create the same object using CreateObject("system.collections.ArrayList"), the for each loop is working fine!
I just realy like to use the dotNET component, because of the nice autocomplete functionallity when writing code. (which is not working when using the CreateObject method)
Am I doing something wrong with create via the dotNET component, or is the for each support simply not available this way?
Thanks in advance for your reply!
I want to use an ArrayList object from the system.collections library, because of it's great member functionallities.
However, when I create such an object as next , I am not able to use For Each loops:
Dim myList
Set myList = dotNET.System_Collections.ArrayList.zctor()
myList.Add "banana"
myList.Add "apple"
For Each element In myList
msgbox element
Next
However, when I create the same object using CreateObject("system.collections.ArrayList"), the for each loop is working fine!
I just realy like to use the dotNET component, because of the nice autocomplete functionallity when writing code. (which is not working when using the CreateObject method)
Am I doing something wrong with create via the dotNET component, or is the for each support simply not available this way?
Thanks in advance for your reply!
Hi Frank, I did a quick bit of research and came up with a theory
CreateObject creates and returns a reference to a COM object
For Each is a VBScript construct
If you inspect myList when it is created using CreateObject, you can view cannot view any properties and methods
If you inspect myList when it is created using dotNET.System_Collections.ArrayList.zctor, you view all properties, fields and methods
My conclusion is that CreateObject and dotNET.System_Collections.ArrayList.zctor create different types of objects and the VBScript For Each construct can only operate on a COM object created by using CreateObject
dotNET.System_Collections.ArrayList.zctor probably creates an instance of a .Net object in which certain operations are not available to native VB Script
This appears to be confirmed as if you use CreateObject to create myList, myList.ToArray will fail whereas if you use dotNET.System_Collections.ArrayList.zctor it will work
Regards,
Phil Baird