Forum Discussion

mst1's avatar
mst1
Occasional Contributor
11 years ago

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!




  • 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

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    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

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Frank, before giving up on using the ArrayList constructor, you may want to look at using an Enumerator as opposed to the VB Script For Each, such as this:


     


    Sub TestArrayList


      Dim myList, iter, element


      


      Set myList = dotNET.System_Collections.ArrayList.zctor


      myList.Add "banana"


      myList.Add "apple"


     


      Set iter = myList.GetEnumerator  


      While iter.MoveNext


        element = iter.get_Current


        Log.Message element  


      Wend


    End Sub


     


    It is slightly more verbose, but this is probably the more "correct" way to create and iterate .Net collections and given that the For Each is just a forward only iterator anyway you're not losing anything.


     


    This will also allow you utilise the autocomplete as per you original post.


     


    Phil

  • mst1's avatar
    mst1
    Occasional Contributor
    Hi Alexei,



    Thanks for your reply.



    I saw this post also, but I did not found an answer in it about why the for loop is not with the dotNET object, while it is working via the CreateObject method.



  • mst1's avatar
    mst1
    Occasional Contributor
    Hi Phil,



    Thanks for your explanation, it sounds logical, and answered my question. Therefor I will use the CreateObject method.



    Kind regards,



    Frank





  • mst1's avatar
    mst1
    Occasional Contributor
    Hi Phil,



    This is a nice workaround for working with the .NET component. However, I think that working with the CreateObject method (so able to use foreach) will result in a more readable script.



    Another advantage which came up in me is that the CreateObject is even a more generic solution, which is evironment (testcomplete) independent.



    When writing code, I now just temporarily use the dotNET component for the autocomplete, and change this to the CreateObject method when the code is finished.



    However, again thanks for your solutions!