Topic+4+-+Visual+Basic+6.0+-+Notes+From+Introduction+To+Arrays

=toc= = INTRODUCTION ON VISUAL BASIC 6.0 =

a computer program is an organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. Without programs, computers are useless. Therefore, programming means designing or creating a set of instructions to ask the computer to carry out certain jobs which normally are very much faster than human beings can do.

The microchips of a CPU can only understand two distinct electrical states, namely, the on and off states, or 0 and 1 codes in the binary system. So, the CPU only understands a combinations of 0 and 1 codes, a language which we called machine language. Machine language is extremely difficult to learn and it is not for us laymen to master it easily. Fortunately, we have many smart programmers who wrote interpreters and compilers that can translate human language-like programs such as BASIC into machine language so that the computer can carry out the instructions entered by the users. Machine language is known as the primitive language while Interpreters and compilers like Visual Basic are called high-level language. Some of the high level computer languages beside Visual Basic are Fortran, Cobol, Java, C, C++, Turbo Pascal, and etc.

= WHAT IS VISUAL BASIC? =

Now, there are many versions of VB exist in the market, the most popular one and still widely used by many VB programmers is none other than Visual Basic 6. VISUAL BASIC is a VISUAL and events driven Programming Language. These are the main divergence from the old BASIC. In BASIC, programming is done in a text-only environment and the program is executed sequentially. In VB, programming is done in a graphical environment. In the old BASIC, you have to write program code for each graphical object you wish to display it on screen, including its position and its color. However, In VB, you just need to drag and drop any graphical object anywhere on the form, and you can change its color any time using the properties windows.
 * VISUAL BASIC ** is a high level programming language which evolved from the earlier DOS version called BASIC. ** BASIC ** means ** B ** eginners' ** A ** ll-purpose ** S ** ymbolic ** I ** nstruction ** C ** ode. It is a very easy programming language to learn. The code look a lot like English Language. Different software companies produced different versions of BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA and so on. However, people prefer to use Microsoft Visual Basic today, as it is a well developed programming language and supporting resources are available everywhere.

= WHAT PROGRAMS CAN BE CODED USING VISUAL BASIC 6.0? =  With VB 6, you can create any program depending on your objective. For example, if you are a college or university lecturer, you can create educational programs to teach business, economics, engineering, computer science, accountancy, financial management, information system and more to make teaching more effective and interesting. If you are in business, you can also create business programs such as inventory management system, point-of-sale system, payroll system, financial program as well as accounting program to help manage your business and increase productivity. For those of you who like games and working as games programmer, you can create those programs as well. Indeed, there is no limit to what program you can create   = WHAT IS AN ARRAY? =

An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the array name and the array element position number. The Individual elements of an array are identified using an index. Arrays have upper and lower bounds and the elements have to lie within those bounds. Each index number in an array is allocated individual memory space and therefore users must evade declaring arrays of larger size than required.

= DECLARING AN ARRAY  =

Arrays occupy space in memory. The programmer specifies the array type and the number of elements required by the array so that the compiler may reserve the appropriate amount of memory. Arrays may be declared as Public (in a code module), module or local. Module arrays are declared in the general declarations using keyword Dim or Private. Local arrays are declared in a procedure using Dim or Static. Array must be declared explicitly with keyword "As".

There are two types of arrays in Visual Basic namely:


 * Fixed-size array : ** The size of array always remains the same-size doesn't change during the program execution.


 * Dynamic array : ** The size of the array can be changed at the run time- size changes during the program execution.

= FIXED SIZED ARRAY  =

When an upper bound is specified in the declaration, a Fixed-array is created. The upper limit should always be within the range of long data type.

Declaring a fixed-array

Dim numbers(5) As Integer

In the above illustration, numbers is the name of the array, and the number 6 included in the parentheses is the upper limit of the array. The above declaration creates an array with 6 elements, with index numbers running from 0 to 5.

If we want to specify the lower limit, then the parentheses should include both the lower and upper limit along with the To keyword. An example for this is given below.

Dim numbers (1 To 6 ) As Integer

In the above statement, an array of 10 elements is declared but with indexes running from 1 to 6.

A public array can be declared using the keyword Public instead of Dim as shown below.

Public numbers(5) As Integer

=   MULTIDIMENSIONAL ARRAYS    = = =

Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two indexes: The first (by convention) identifies the element's row and the second (by convention) identifies the element's column.

Tables or arrays that require two indexes to identify a particular element are called two dimensional arrays. Note that multidimensional arrays can have more than two dimensions. Visual Basic supports at least 60 array dimensions, but most people will need to use more than two or three dimensional-arrays.

The following statement declares a two-dimensional array 50 by 50 array within a procedure.

Dim AvgMarks ( 50, 50)

It is also possible to define the lower limits for one or both the dimensions as for fixed size arrays. An example for this is given here.

Dim Marks ( 101 To 200, 1 To 100)

An example for three dimensional-array with defined lower limits is given below.

Dim Details( 101 To 200, 1 To 100, 1 To 100)

= STATIC AND DYNAMIC ARRAY  =

Basically, you can create either static or dynamic arrays. Static arrays must include a fixed number of items, and this number must be known at compile time so that the compiler can set aside the necessary amount of memory. You create a static array using a Dim statement with a constant argument:

' This is a static array.

Dim Names(100) As String

Visual Basic starts indexing the array with 0. Therefore, the preceding array actually holds 101 items.

Most programs don't use static arrays because programmers rarely know at compile time how many items you need and also because static arrays can't be resized during execution. Both these issues are solved by dynamic arrays. You declare and create dynamic arrays in two distinct steps. In general, you declare the array to account for its visibility (for example, at the beginning of a module if you want to make it visible by all the procedures of the module) using a Dim command with an empty pair of brackets. Then you create the array when you actually need it, using a ReDim statement:

' An array defined in a BAS module (with Private scope)

Dim Customers As String

...

Sub Main

' Here you create the array.

<span class="text2" style="font-family: 'Courier New',Courier,mono; font-size: 10pt;">ReDim Customer(1000) As String

<span class="text2" style="font-family: 'Courier New',Courier,mono; font-size: 10pt;">End Sub

If you're creating an array that's local to a procedure, you can do everything with a single ReDim statement:

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">Sub PrintReport

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">' This array is visible only to the procedure.

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Customers(1000) As String

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">' ...

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">End Sub

If you don't specify the lower index of an array, Visual Basic assumes it to be 0, unless an Option Base 1 statement is placed at the beginning of the module. My suggestion is this: Never use an Option Base statement because it makes code reuse more difficult. (You can't cut and paste routines without worrying about the current Option Base.) If you want to explicitly use a lower index different from 0, use this syntax instead:

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Customers(1 To 1000) As String

Dynamic arrays can be re-created at will, each time with a different number of items. When you re-create a dynamic array, its contents are reset to 0 (or to an empty string) and you lose the data it contains. If you want to resize an array without losing its contents, use the ReDim Preserve command:

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Preserve Customers(2000) As String

When you're resizing an array, you can't change the number of its dimensions nor the type of the values it contains. Moreover, when you're using ReDim Preserve on a multidimensional array, you can resize only its last dimension:

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Cells(1 To 100, 10) As Integer

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">...

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Preserve Cells(1 To 100, 20) As Integer ' This works.

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">ReDim Preserve Cells(1 To 200, 20) As Integer ' This doesn't.

Finally, you can destroy an array using the Erase statement. If the array is dynamic, Visual Basic releases the memory allocated for its elements (and you can't read or write them any longer); if the array is static, its elements are set to 0 or to empty strings.

You can use the LBound and UBound functions to retrieve the lower and upper indices. If the array has two or more dimensions, you need to pass a second argument to these functions to specify the dimension you need:

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">Print LBound(Cells, 1) ' Displays 1, lower index of 1st dimension

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">Print LBound(Cells) ' Same as above

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">Print UBound(Cells, 2) ' Displays 20, upper index of 2nd dimension

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">' Evaluate total number of elements.

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">NumEls = (UBound(Cells) _ LBound(Cells) + 1) * _

<span style="background-color: #ffffff; color: #333333; font-family: 'Courier New',Courier,mono;">(UBound(Cells, 2) _ LBound(Cells, 2) + 1)