Arrays

Reading/Writing to Arrays

You can fill elements of an array very easily. You can use the following syntax:

ArrayName(Element) = Value

Where element is a valid element. The following code declares gArray and fills elements 1,2 and 3 with names.

Dim sArray(1 To 10) As String
sArray(1) = "Paul"
sArray(2) = "Fred"
sArray(3) = "Colin"

Likewise, you can access the value in the array in the same way. The following code displays a message box containing the 3rd value in sArray:

Msgbox sArray(3)

As I said at the start of this tutorial, this can be very beneficial as you can access different values in the array using code. For example, if you hard coded 3 variables:

Dim sName1 As String
Dim sName2 As String
Dim sName3 As String

you would be limited to 3 names, and if you wanted to access them, you would have to write different code for accessing each variable, as you can't type

Msgbox (sName & lNum)

to access sName1, if lNum is 1. With Arrays, you can:

Msgbox sNames(lNum)

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup