Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 68,806 times

Contents

Related Categories

Arrays - Reading/Writing to 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)

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [19] Arrays

    Posted by elyssa83 on 15 May 2007

    I'm a bit confuse. I would like user to insert how many data they want to put in the array.


    When I declare the array like this


    Dim subjArr( ) As DataType


    How should I write ...

  • Re: Array constants and assigning to all values

    Posted by pcmattman on 20 Dec 2006

    It would work if you used Dim instead of Const. A constant array is not possible in VB and I really don't see the point. Just use a normal array and make sure that it is not modified.

  • Thanks James

    Posted by csm1yy on 17 Nov 2003

    Thanks James

    This is a good Array Tutorial for VB Newbies like me.

  • Here ya go.

    Posted by STmindfulORM on 29 Apr 2003

    These guys have a perfect working example. The randomizer isn't truly random but completely adequate for basic purposes. I couldn't get it to work as a function, but it worked great when I simply used...

  • Array constants and assigning to all values

    Posted by HyperHacker on 03 Apr 2003

    How do I make a constant array? When I try "const data(1 to 72) as byte" it doesn't work, where it would if I used Dim rather than const. Also, isn't there some way to set a value to the whole thing? ...