The usage of preserve in VB

Source: Internet
Author: User

Note: This article was reprinted from: http://zhidao.baidu.com/question/161401549.html

The ReDim statement is used to define or redefine the size of a dynamic array that was previously declared with a Private, public, or Dim statement with empty parentheses (no dimension subscript).
You can use the ReDim statement to repeatedly change the elements of an array and the number of dimensions, but you cannot use ReDim to change an array to a different data type after you have defined it as a data type, unless it is an array contained in a Variant. If the array is indeed contained in a Variant and does not use the Preserve keyword, you can use the AS type clause to change the type of its elements, but it is not allowed to change any data type in the case of using this keyword.
If you use the Preserve keyword, you can only redefine the size of the last dimension of the array, and you cannot change the number of dimensions at all. For example, if the array is one-dimensional, you can redefine the size of the dimension because it is the last dimension and the only one. However, if the array is a two-dimensional or more dimension, only the last dimension will be changed to retain the contents of the array at the same time. The following example shows how to increase the last dimension size for an existing dynamic array without erasing any of the data contained therein.
ReDim X (10, 10, 10) ... ReDim Preserve X (Ten, ten)
Similarly, when using Preserve, the size of the array can only be changed by changing the upper bounds, and changing the nether will result in an error.
If you change the array to small, the data in the deleted element is lost. If you pass an array to a procedure by address, do not redefine the size of the array's dimensions within the procedure.
when the variable is initialized, the numeric variable is initialized to 0, the variable-length string is initialized to a 0-length string (""), and the fixed-length string is filled with 0. The variant variable is initialized to Empty. Each element of a variable of a user-defined type is initialized as a separate variable. Before you use a variable that references an object, you must use the Set statement to assign an existing object to the variable. Before being assigned, the declared object variable has a specific value of nothing, which indicates that the variable does not point to an instance of any object.
CAUTION If the variable used by the ReDim statement does not exist at the module level or at the procedure level, the statement is equivalent to a declaration statement. If a variable of the same name is created later in a broader scope, even if you use Option Explicit,redim, the variable declared later will not cause a compilation error. To avoid this conflict, you should not use ReDim as a declaration statement, but only as a statement that defines the size of the array.
Note To change the size of the array that the variant contains, you must explicitly declare the variant variable before attempting to change its array size.
preserving the contents of a dynamic array each time the ReDim statement is executed, the values currently stored in the array are all lost. Visual Basi Resets the value of the array element to empty (for a Variant array), to 0 (to the 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 save memory. Sometimes you want to change the size of the array without losing the data in the array. This can be done using a ReDim statement with the Preserve keyword. For example, use the UBound function to refer to the upper bounds, make the array expand, add an element, and the value of the existing element is not lost:
ReDim Preserve Dynarray (UBound (Dynarray) + 1)
when you use the Preserve keyword, you can only change the upper bound of the last dimension in a multidimensional array, and if you change the lower bound of another dimension or the last dimension, the runtime will have an error. So this can be programmed:
ReDim Preserve Matrix (UBound (Matrix, 2) + 1)
This is not programmed:
ReDim Preserve Matrix (UBound (Matrix, 1) + 1, ten)

The usage of preserve in VB

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.