Pages

Friday, March 4

Arrays in QTP - Part 1

Hello Friends! In this post, we'll brush-up our understanding about vbscript arrays and to use arrays in QTP.


Fisrt thing first.. What is an Array?

- An array variable is used to store multiple values in a single variable.

- Array can contain a series of values of same data type.

- An array is a memory space referred by a common name.


Please note that VBScript explicitly supports only one data type - ‘Variant’. A variant can store a number as well as a string.

In vbscript, Arrays are of two types..


1. Static or Fixed Sized Array
2. Dynamic or Variable Sized Array


Both type of arrays can be of single dimensional or multi-dimensional. So to make the things simple, we can write this like...


1. Static or Fixed Sized Array
 
    1.1. Single dimensional Static Array   'Exp - Dim myArray(4)
    1.2. Multi-dimensional Static Array   'Exp - Dim myArray(3,4)


2. Dynamic or Variable Sized Array

    2.1. Single dimensional Dynamic Array
    2.2. Multi-dimensional Dynamic Array


Sounds batter? Okey.. great!!


Dim myArray(4)    'declaring array in vbscript


In the above example, an array containing 5 elements is declared:


But why 5? The number shown in the parentheses is 4!!

Ya! But array index actually starts from zero, so this array contains 5 elements. This is a fixed-size array.


You assign data to each of the elements of the array like this:

myArray(0)= 21
myArray(1)= 42
myArray(2)= 51
myArray(3)= 33
myArray(4)= 15


It was one way of declaring and assigning values to an array.
Here is the another way...

Dim myArray
myArray = Array(21,42,51,33,15)   'integer values
myArray = Array("val1","val2","val3","val4","val5")   'string values
myArray = Array("1",2,"val3",4,"val5")   'mix of num and string values

As I mentioned above as well, VBScript explicitly supports only one data type - ‘Variant’. A variant can store a number as well as a string.

It was the VBScript Array Method of Creating Arrays.

Array we have discussed above is Static Array - Single dimensions. A Static Array -

- has a pre-defined number of elements.
- size of a static array cannot be altered at run time.

Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:

Dim myArray(4,6)

In the above example, an array containing 5 rows and 7 columns has been declared.

Calculating the Size of Arrays:

ubound method is used to count the length of array. It returns the highest index number of the array.
But remember, the actual size of the array is highest index number plus one!! (since the array starts at index number 0).

Dim myArray(4),  len1
len1 = ubound(myArray)  '4
msgbox "Length of the Array is: " & len1+1   'output is 5


ubound method can be used to count the size of a multidimensional array also, but this time it's slightly different..

The dimension number needs to be included in ubound.

Dim myArray(4,6)
msgbox ubound(myArray, 1) 'shows highest index of the first dimension - 4
msgbox ubound(myArray, 2) 'shows highest index of the second dimension - 6

Tired! me too...!!


Another type of array is Dynamic array. In next part of post, we'll explore dynamic arrays and other good stuff about using arrays.

Please post your comments and tell me how you find this article!!

33 comments:

  1. Hey Abhi...
    Its quite helpful and elaborative just like class room session.
    Good stuff!!

    ReplyDelete
  2. @Smiles
    thanks!! keep reading the good stuff!

    ReplyDelete
  3. thanks for valuable information...............

    ReplyDelete
  4. I have gone through multiple sites for learning an array in QTP , and it was not of much use , but the way you have explained it is in simple words and easy to understand, have never written any commentc for anything till date ,and this is the 1st time ever Im writting a comment as I was really impressed by your explanation.

    Keep up the good work and expecting more on QTP scripting.

    Thanks

    ReplyDelete
  5. Hi
    I don't ur name
    But what u r explaining is very useful n very easy to understand
    Thanks

    ReplyDelete
  6. Supreb. Keep posting.

    ReplyDelete
  7. simply great dude! keep up the good work!

    ReplyDelete
  8. hi Abhi,

    Thanks for valuable information.

    can pl explain how can we enter alues in multi dimentional static array

    ReplyDelete
  9. Thank you for the awesome information... guess what I love the pink backgroung :D

    ReplyDelete
  10. so much impressed and easily understandable...

    ReplyDelete
  11. how to click a multiple object dynamically in qtp example i want to click multiple webbutton dynamically?

    ReplyDelete
  12. Thanks for your information, very useful :D

    ReplyDelete
  13. can you please explain wat is ubound and its function

    ReplyDelete
  14. Even i follow the exact process...

    ReplyDelete
  15. its really helpful for the people who really likes to learn QTP.
    its better compared to class room.......
    great work Abhi........

    ReplyDelete
  16. Abhi,

    You are just..too good.

    ReplyDelete
  17. @Priya - thanks for your words! I'm glad you like it.

    ReplyDelete
  18. nice explanation

    ReplyDelete
  19. Can we have apply a filter on two dimensional array?

    ReplyDelete

  20. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    Php course in chennai

    ReplyDelete
  21. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    Click Here : used motor grader cat 140h cca01812 for sale

    ReplyDelete
  22. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  23. Great Post! Thanks for your Share.


    Quality Onesie: Kigurumi Onesie Pajamas For Adults And Kids

    Qualityonesie offers Pikachu, Disney, Anime, Hello Kitty, Animals, Winnie the Pooh, Stitch, Unicorn Kigurumi Onesie Pajamas for Adults and Kids.

    ReplyDelete