• We just launched and are currently in beta. Join us as we build and grow the community.

Arrays

labollo

SEO Troubleshooter
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
90
Likes
91
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In programming, there are many cases that we need to reuse or we want to store many values in a single variable, but the problem is that variables can only hold one value. On the other hand we will be using an array. An array is a series of objects, with the same type and size. Each object in an array is called an element of an array. It's either you have an array of strings or characters, integers or any that has defined data type.

The main use of an array is to organize homogeneous data together as a group to perform operations on data in a collective manner since traversal and retrieval become easy.

This time, we’re going to create a new windows form application project and save it as “Arrays”. Then add a button and add the following code:

Take note, arrays is accessed by their index number and an array always starts from zero.
  1. 'declare array as a string with six index
  2. Dim

    words(

    6

    )

    As

    String
  3. 'here, we assign values to specific index of an array
  4. words(

    0

    )

    =

    "Visual"
  5. words(

    1

    )

    =

    "Basic"
  6. words(

    2

    )

    =

    "programming"
  7. words(

    3

    )

    =

    "is"
  8. words(

    4

    )

    =

    "fun"
  9. words(

    5

    )

    =

    "to"
  10. words(

    6

    )

    =

    "learn!"

  11. 'display a value based on the specific index
  12. MsgBox

    (

    letters(

    6

    )

    )

Next, we’re going to display a specific value based on the user input. To do this add a textbox to your form. Then modify your code and it will look like as shown below.

  1. 'declare array as a string with six index
  2. Dim

    words(

    6

    )

    As

    String
  3. 'here, we assign values to specific index of an array
  4. words(

    0

    )

    =

    "Visual"
  5. words(

    1

    )

    =

    "Basic"
  6. words(

    2

    )

    =

    "programming"
  7. words(

    3

    )

    =

    "is"
  8. words(

    4

    )

    =

    "fun"
  9. words(

    5

    )

    =

    "to"
  10. words(

    6

    )

    =

    "learn!"

  11. 'declare new variable as integer
  12. Dim

    i As

    Integer
  13. 'assign value from textbox1 to a variable
  14. i =

    TextBox1.

    Text
  15. 'display a value based on the specific index
  16. MsgBox

    (

    words(

    i)

    )

Then you can now test your application by pressing “F5”.

Book traversal links for Arrays

  • ‹ Arithmetic Operators
  • Up
  • CheckBox Control ›

 

442,401

317,942

317,951

Top