Static array declaration and example exercises

Source: Internet
Author: User

Definition and declaration of Arrays

Before using an array, you must declare an array. You can also use an implicit declaration method. The method for declaring an array is the same as that for declaring various types of variables. It can be declared using dim, private, public, or static, except that the upper and lower bounds of the array must be set when declaring an array, that is, the start value and stop value of the array subscript index.

(1) Static array:

The statement syntax is as follows:

Dim │ private │ public │ static
Array name ([subscript lower bound to] subscript Upper Bound)
[As data type]

Note:

The array name is followed by circular arc "(
.

If the lower bound of the subscript of the array is not specified, the system defaults to 0. The upper bound of the subscript of the array uses long data, and the upper bound cannot exceed 2,147,483,647 in VB.

The lower bound cannot be greater than the lower bound;

You can use the variable name or constant name (and real number) as the subscript boundary. When the current object boundary is a constant name, the array size is fixed (static array). When the current object boundary is a variable name, the array size can be dynamically defined (dynamic array ).

Example:

Dim A (10) as integer
'Indicates an array named A. The default subscript is 0, and the upper bound is 10. An array with 11 integer elements, that is, from a (0), A (1) to a (10 );

Dim B (1
To 20) as integer 'indicates that the array name is B, the lower bound of the subscript is 1, the upper bound of the subscript is 20, and there are 20 integer elements;

Dim birthday (1 to 10) as date
'Indicates that birthday is a date array with indexes ranging from 1 to 10.

Dim dayarray (50) 'indicates that the variable is a variant array with 51 index elements;

Public class (10) as string
'Declare a global integer array class with the length of 11;

Dim matrix (3, 4) as integer
'Indicates that the variable is a two-dimensional integer array;

Dim mymatrix (1 to 5, 4 to 9, 3 to5) as double
'Indicates that the variable is a three-dimensional double array with the upper and lower bounds specified;

You can use a loop statement to assign an initial value to an array. For example:

Dim I as integer
For I = 0 to 11
C (I) = I
'C (0), C (1 ),...... C (11)
Next I

Example:The following exercise uses a one-dimensional public array named temperatures to record the daily maximum temperature for seven days a week.

Interface:

Add three command buttons on the form, and set the autoredraw attribute of the form to true. -- When the print method is used to display information in the form, the autoredraw attribute of the form is always set to true. In this way, when the form is overwritten by another window and then displayed again, Visual Basic will automatically re-draw the screen.

Note: The "option explicit" statement is not forcibly declared.

Code:

Dim temperatures (6) as Variant

Private sub commandementclick ()
CLS
Prompt $ = "Enter the high temperature ."
For I % = 0 to 6
Title $ = "day" & I %
Temperatures (I %) = inputbox (prompt $, title $)
Next I %
End sub

Private sub command2_click ()
CLS
Print "high temperatures for the week :"
Print
For I % = 0 to 6
Print "day"; I %, temperatures (I %)
Total! = Total! + Temperatures (I %)
Next I %
Print
Print "average high temperature:"; total! /7
End sub

Private sub command3_click ()
End
End sub

(This program demonstrates how to store and process a set of related values using arrays in the program. By using the inputbox function and for... next loop, the temperature value is assigned to the array. The cyclic counter is used to index each element in the array. The array content is displayed in the form using the for... next loop and print (print) method, and the average maximum temperature is calculated and displayed.

You can also use the for loop nesting to effectively process multi-dimensional arrays. Example:

Dim I as integer, J as integer
'Set the counter
Dim class (14) as string 'defines the class name Array
Dim studentname () as string 'defines the Student name Array
......
'Assign a value to the class Array
...... 'Assign a value to the student Array
For I = 0 to 14
For J = 0 to 35
If studentname (I, j) = "James"
Then
Msgbox "James is in" Ten class (I)
End if
Next J
Next I

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.