Forum Discussion

venkat001's avatar
venkat001
Occasional Contributor
12 years ago

Declaring array in Testcomplete using VBscript

I need to declare an array of 7 integer elements using VB script, I getting "error 110: syntax error while lexinf character:"

Tried below codes:

    1. Dim arr={1,2,3,4,5,6,7}

    2.Dim arr(6)={1,2,3,4,5,6,7}

    3.Dim arr() = {1,2,3,4,5,6,7}





Regards,

venkateswaran K

6 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi,



    Dim arr(7)

    arr(0)=1

    arr(1)=2

    arr(2)=3

    arr(3)=4

    arr(4)=5

    arr(5)=6

    arr(6)=7
  • venkat001's avatar
    venkat001
    Occasional Contributor
    Thanks Alexei,


    Any other method of declaring. F.Ex. if we get  'n' no of elements from the user and wants to process each element by reading it from for loop and do some action depends on case logic.


    Ex:


     User Input: 1,5,8,6,9,10....n


    Code:


    Sub Expectedinfo( array_input)


        Dim arr()


         arr()= array_input 

        a=ubound(arr())                ' to identify array size  


        For i=0 to a

        '' Statements


        Next


    End Sub





    Regards,


    Venkates K










  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
     Well if you get a string like this: "1,2,3,4,5,6", you can use Split() to transform that into an array.



    If you receive an array why would you need to cast it?
  • venkat001's avatar
    venkat001
    Occasional Contributor
    Thanks Hugo

    Well, split() function is good when i know my array with pre-defined delimiter, suppose if i want read cell values from excel and want to put it an array elements. In that case we may not know array size so we need to read from for loop.



    Ex.

    Sub array

        Dim arr()

        For i=0 to 10

           a(i)= Excel.cells(i,1) ' getting error in a(i)

        Next

    End sub



    Regards,

    venkates k

  • venkat001's avatar
    venkat001
    Occasional Contributor
    Thanks to all.

    Got solution. when we declare dynamic array DIM arr() and try to asign the value before declaring the array dimension. we will get error message.

    Ex. To read Excel data

    Sub Temp

        Dim arr()

        ''' Statements to be added '''''

        x= Excel.Application.Activesheet.Usedrange.rows.count

        y= Excel.Application.Activesheet.Usedrange.columns.count

        ReDim arr(x)



        For i=0 to x

            Forj=0 to

            arr(i,j)=Excel.cells(i+1,j+1)

            Next

        Next

    End Sub



    Regards,

    Venkates K