Forum Discussion

nassim's avatar
nassim
Occasional Contributor
14 years ago

How to create an array of size eg 9 where each member is an object

Hi all,

I try to create an array of fixed size (for example 8) where each member is an object which has 4 string members. I would like to do it with VB scripting. The next question would be how to access for example the third member (let us name it urlStr) of the 2nd object in the array.

Thank you very much in advance,

Nassim

6 Replies

  • AlexKaras's avatar
    AlexKaras
    Community Hero
    Hi
    Nassim,



    Something like this (untested):



    Dim arr

    Redim arr(8)



    Set arr(0) = obj1

    ...

    Set arr(7) = obj8

    ...

    Log.Message arr(1).urlStr

    ...

    Set objFromArray = arr(7)

    Log.Message objFromArray.urlStr
  • nassim's avatar
    nassim
    Occasional Contributor
    Hi,

    Thank you for your reply.

    In Project Explorer, I chose ODT->CLASSES and defined a class SDP with properties hh and cuts (I found no way to define the types.)

    Afterwards, I wrote these two Subs:



    Sub createSPArray()

      Dim strSPArray

      Redim strSPArray(8)   

    End Sub



    Sub populatestrSPArray(i, h)

      j = aqConvert.StrToInt(i)

      Set testObj = ODT.Classes.New("SDP")

      testObj.hh = h

      testObj.cuts = Project.Variables.clientTimeStamp

      Set strSPArray(j) = testObj

    End Sub



    In my keyword test, I call these Subs. However, at the line: Set strSPArray(j) = testObj

    I become an exception: Laufzeitfehler in Microsoft VBScript
    Typen unverträglich: 'strSPArray'Laufzeitfehler in Microsoft VBScript

    Typen unverträglich: 'strSPArray'



    Does anyone have an idea?

    Thanks
  • Hi,



    strSPArray doesn't exist inside 'populatestrSPArray'. It is declared in another function, and it is out of scope.
  • nassim's avatar
    nassim
    Occasional Contributor
    Hi,

    trying to solve the possible scope problem, I defined a variable in my keywordtest as: strSPArray (for the type, I found Object type nearest to an array of objects, although I am not sure at all)



    I changed the Sub populatestrSPArray:

    Sub populatestrSPArray(i, h)

      j = aqConvert.StrToInt(i)

      Set testObj = ODT.Classes.New("SDP")

      testObj.hh = h

      testObj.cuts = Project.Variables.clientTimeStamp

      Set strSPArray(j) = testObj

    End Sub

    I also wanted to add the line Redim KeywordTests.refEnvUsageTestFF.Variables.strSPArray(8)    but the syntax is not correct (shown to be red in script by TestComplete)

    Now I call just the Sub populatestrSPArray in my test.

    An Exception occurs again: (translated:) Run time exception. The types are not compatible: strSPArray

    Does anyone have an idea?

    Why can a Keywordtest variable not redim(med) in script?

    Thanks
  • Hi,



    VBScript arrays are not objects. If you want to have a dynamic array declared in KDT variables, use a variable of the table type.