VB variables, constants and data types and process overview (eight)

Source: Internet
Author: User
Tags arrays empty integer variables

Using a loop to manipulate an array
You can use a for loop to nest a valid processing multidimensional array. For example, in Matrixa, you assign values based on the position of each element in the array:
Dim I As Integer, J as Integer
Static Matrixa (1 to ten, 1 to ten) as Double
For I = 1 to 10
For J = 1 to 10
Matrixa (i, j) = I * ten + J
Next J
Next I
For more information about loops, see the "Looping Structure" section later in this chapter.

Dynamic array
How big an array should be, sometimes it may not be known. So hopefully it will be able to change the size of the array at runtime.
A dynamic array can change size at any time. In Visual Basic, dynamic arrays are the most flexible and convenient way to help manage memory efficiently. For example, you can use a large array for a short time, and then release the memory space to the system without using the array.
If you do not use a dynamic array, declare an array, as large as possible, and then erase the unnecessary elements. However, excessive use of this method can result in slower operating environments for the memory.
To create a dynamic array, follow these steps:
1. (If you want the array to be a common array, declare the array with the public statement, or, if you want the array to be a module-level, declare the array with the DIM statement at the module level, or, if you want the array to be a local array, declare the array with a Static or Dim statement Attach an empty dimension table to an array so that the array is declared as a dynamic array.
Dim Dynarray ()
2. Allocate the actual number of elements using the ReDim statement.
ReDim Dynarray (X + 1)
The ReDim statement can only appear in the procedure. Unlike the Dim statement, the Static statement, the REDIM statement is an executable statement in which the application performs an action at run time. The REDIM statement supports such a syntax, which is the same as the syntax used in a fixed array. For each dimension, each REDIM statement can change the number of elements and the upper bound. However, the dimensions of the array cannot be changed.
ReDim Dynarray (4 to 12)
For example, with the first declaration of a dynamic array established at the module level Matrix1:
Dim Matrix1 () as Integer
Then, allocate space to the array in the procedure:
Sub Calcvaluesnow ()
.
.
.
ReDim Matrix1 (19, 29)
End Sub
The REDIM statement here assigns a 20x30 integer matrix to the matrix (the total element size is 600). There is also a way to set the bounds of a dynamic array with a variable:
ReDim Matrix1 (X, Y)
Note You can assign a string to a variable-size byte array. A byte array can also be assigned to a variable-length string. Be sure to note that the number of bytes in the string changes with the platform. The same string is twice times the number of bytes on the Unicode platform as it is on a non-Unicode platform.

Preserve the contents of a dynamic array
Each time the ReDim statement is executed, the values currently stored in the array are all lost. VisualBasic resets the value of an array element to Empty (to a Variant array), to 0 (to a numeric array), to a zero-length string (to a string array), or to nothing (for an array of objects).
This is useful when you are preparing an array for new data, or if you want to reduce the size of the array to conserve memory. Sometimes you want to change the size of an array without losing the data in the array. This can be done using the ReDim statement with the Preserve keyword. For example, using the UBound function to reference the upper bounds, the array is enlarged, an element is added, and the value of an existing element is not lost:
ReDim Preserve Dynarray (UBound (Dynarray) + 1)
When you use the Preserve keyword, you can only change the upper bounds of the last dimension in the multidimensional array, and if you change the lower bound of the other dimension or the last dimension, the runtime will be faulted. So you can program this way:
ReDim Preserve Matrix (UBound (Matrix, 2) + 1)
Instead of programming this way:
ReDim Preserve Matrix (UBound (Matrix, 1) + 1, 10)
For more information about dynamic arrays, see "ReDim functions" in the language reference. For an array of objects, see Chapter Nineth, "Programming with objects."

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.