Vb-Notes1
Vb-Notes1
Declaring Arrays
Unlike simple variables, arrays must be declared with the Dim (or Public, or Private) statement
followed by the name of the array and the index of the last element in the array in
parentheses for example:
Names(0)= "John"
Ages(0) =34
Names(1)= "Sam"
Ages(1) =38
Names(19)= "Hedric"
Ages (19) =45
Dynamic Arrays
Sometimes we will not know how large an array to create. The earlier approach was to make it
large enough to hold the maximum number of data. This will result in on an average, most of the
array will be empty. To avoid this we can declare a dynamic array. The size of a dynamic array can
vary during the execution of the software program.
With a dynamic array, we can discard the data and return the resources it occupied to the system.
To create a dynamic array, declare it as usual with the Dim statement, Public or Private but don´t
specify its dimensions:
Later in the program, when we know how many elements we want to store in the array, use the
ReDim statement to redimension the array, this time to its actual size. In the following example,
UserCount is a user-entered value:
ReDim DynArray(UserCount)
The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is
executable, it forces the application to carry out an action at runtime. Dim statements aren´t
executable, and they can appear outside procedures.
A dynamic array also can be redimensioned to multiple dimensions. Declare it with the Dim
statement outside any procedure as follows
and then use the ReDim statement in a procedure to declare a three-dimensional array:
ReDim Matrix(9,9 , 9)
Note that the ReDim statement can´t change the type of the array that´s why the As clause is
missing from the ReDim statement. Moreover, subsequent ReDim statements can change the
bounds of the array Matrix but not the number of its dimensions. For example, we can´t use the
statement ReDim Matrix(99, 99) later in your code. Once an array has been redimensioned once, its
number of dimensions can´t change. In the preceding example, the Matrix array will remain three-
dimensional through the course of the application.
The ReDim statement can be issued only from within a procedure. In addition, the array to be
redimensioned must be visible from within the procedure that calls the ReDim statement.
ReDim DynArray(UserCount)
5.Write a program with a good interface in visual Basic to print first 20 Fibonacci
numbers.
Private Sub Command1_Click()
Dim x, g, n, i, sum As Integer
n = 20
x=0
y=1
Print x
Print y
For i = 3 To n
sum = x + y
Print sum
x=y
y = sum
y = sum
Next i
End Sub
6. a)
3. we can use checkboxes with in the list can't use checkboxes within combo boxes
box.
4.
1.
Option Button Checkbox
Select your Highest Educational qualification: Select the games you can play:
B.Sc Hockey
M.Sc Cricket