Arrays


© Swapna Kamat
Articles in this Topic    Discussions in this Topic

Arrays in VB

We worked along with loops last time. Lets get into the actual use of looping today. Though they are used for a variety of reasons, the main application of looping structures is the access of arrays.

What Is an Array?

A collection of similar variables, in which each has the same name and all are of the same type, is an array. Remember that a variable can be thought of as a cup that holds an unknown value or an always changing value 

Think of an array, then, as a collection of cups. Each cup in the collection can hold the same type of data, and every cup in the collection has the same name. Each cup within the collection is an element and has a number assigned to it that reflects its position within the collection. The first element of an array usually has a position number of 0 (zero).

Arrays can be different sizes. One array might have three elements, another might have 30 elements, and it's even possible for an array to have no elements at all--only the possibility of having elements that are created at a later time.

Declaring Arrays

You can declare, or dimension, an array in two ways: as you would a single variable and by using the To keyword.

Declaring an Array like Declaring a Single Variable

To declare an array as you would a single variable, you use the following syntax:

Dim|Public|Private ArrayName(Subscript) As DataType

In this syntax,

  • Dim, Public, and Private are Visual Basic keywords that declare the array and its scope. If you use Dim, the array is private to the procedure in which it is declared. Public makes the array visible from anywhere in the program, and Private (within the General section of a form or module) makes the array visible only to the form or module in which it's declared. If you use Dim within a module's procedure, the array will be available to only that procedure, but if you use Dim in the module's Declarations section, the array will be available to all procedures within the module.
  • ArrayName is the name of the array.

Dim newarray(4) As Integer

To assign a value to each element in the array newarray, you would use the following:

newarray(0) = 90
newarray(1) = 34
newarray(2) = 27
newarray(3) = 10
newarray(4) = 89

To change the value of the fourth element of the array newarray from 10 to 45, you would do as follows:

newarray(3) = 45

In the next article, we shall study manipulation of arrays and

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

1.   Oct 3, 2002 9:44 PM
hi swapna,
I am trying to write a socket programming in VB6.0.I dont know how to do it.So please help me in writing this application.Kindly reply me with a solution.

...

-- posted by nareshkumar76





Join the latest discussions

For a complete listing of article comments, questions, and other discussions related to Swapna Kamat's Learning Visual Basic topic, please visit the Discussions page.