Skip to main content

Arrays


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.  
  12. 'display a value based on the specific index
  13. 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.  
  12. 'declare new variable as integer
  13. Dim i As Integer
  14. 'assign value from textbox1 to a variable
  15. i = TextBox1.Text
  16. 'display a value based on the specific index
  17. MsgBox(words(i))

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

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byrina (not verified)on Wed, 05/14/2014 - 10:43

I have a problem in my on going project which is I cannot save the item from listbox going to ms access.how can i do that?please help me.....

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.