Functions and usage of Dim statements in VB

Source: Internet
Author: User


  Syntax

  Dim[WithEvents] Varname [([Subscripts])] [As[New] Type] [,[WithEvents] Varname [([Subscripts])] [As[New] Type]...

  DimThe statement syntax includes the following parts:


Part Description
WithEvents Optional. Keyword, indicating that varname is an object variable used to respond to events triggered by ActiveX objects. It is valid only in the class module. UseWithEventsCan declare any required single variable, but cannot useWithEventsCreate an array.NewAndWithEventsCannot be used together.
Varname Required. The name of the variable, which follows the standard variable naming conventions.
Subscripts Optional. The dimension of the array variable. up to 60-dimensional multi-dimensional arrays can be defined. The subscripts parameter uses the following syntax:
  [LowerTo] Upper [,[LowerTo] Upper]...
  If lower is not explicitly specified, the lower bound of the array isOption BaseStatement control. If notOption BaseStatement, the lower bound is 0.
New Optional. You can implicitly create keywords for an object. If you useNewObject variable, the instance of the object will be created when the variable is referenced for the first time, so you do not need to useSetStatement to assign a value to the object reference.NewThe keyword cannot declare any internal data type variables, as well as the instance of the dependent object, orWithEvents.
Type Optional. The data type of the variable. It can be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not supported currently), Date, String (for variable-length strings ),String* Length (a string with a fixed length), Object, Variant, user-defined type, or Object type. Each declared variable must be a separateAsType clause.

  Description

的 Variables declared with Dim at the module level are available to all procedures in the module. Variables declared at the procedure level are available only within the procedure.

You can use Dim statements to declare the data type of a variable at the module or procedure level. For example, the following statement declares a variable of type Integer.

Dim NumberOfEmployees As Integer

Di You can also use Dim statements to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.

Dim X As New Worksheet

If you do not use the New keyword when defining an object variable, you must use the Set statement to assign the variable that references the object to an existing object before using the variable. Before the variable is assigned a value, the declared object variable has a specific value of Nothing, which indicates that the variable does not point to any object instance.

You can also use Dim statements with empty parentheses to define dynamic arrays. After you define a dynamic array, you can use ReDim statements within a procedure to define the dimensions and elements of the array. An error occurs if you try to redefine the dimensions of an array whose size has been explicitly defined in a Private, Public, or Dim statement.

If you do not specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.

初始化 When you initialize a variable, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with 0. Variant variables are initialized to Empty. Each element of a user-defined variable is initialized as a separate variable.

Note When using Dim statements in procedures, Dim statements are usually placed at the beginning of a procedure.


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.