Table of contents

Type 语句Type Statement

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

模块级别用来定义用户定义的数据类型包含一个或多个元素。Used at module level to define a user-defineddata type containing one or more elements.


[专用 | 公共]类型varname[ Private | Public ] Typevarname
无法识别[ ( [下标] ]作为类型elementname [ ( [ subscripts ] ) ] Astype
[[ ( [下标] ] 的_无法识别_类型][ elementname [ ( [ subscripts ] ) ] Astype ]
. . .

端面类型End Type
类型语句语法具有下列组成部分:The Type statement syntax has these parts:

公共Public可选。用于声明用户定义的类型可供在所有项目中的所有模块的所有过程Optional. Used to declare user-defined types that are available to allprocedures in allmodules in allprojects.
私有Private可选。用于声明用户定义的类型可仅在模块内进行声明的位置。Optional. Used to declare user-defined types that are available only within the module where the declaration is made.
varnamevarname必填。用户定义类型; 名称遵循标准变量命名约定。Required. Name of the user-defined type; follows standard variable naming conventions.
无法识别elementname必填。用户定义的类型的元素的名称。元素名称还按照标准变量命名规则,不同之处在于,可以使用关键字s。Required. Name of an element of the user-defined type. Element names also follow standard variable naming conventions, except that keywords can be used.
下标subscripts时在_较低_,没有明确说明,数组的下限控制选项基本语句。如果任何选项基本语句存在,下限为零。When not explicitly stated in lower, the lower bound of an array is controlled by the OptionBase statement. The lower bound is zero if no OptionBase statement is present.
类型type必填。数据类型的元素;可能是字节布尔值整数货币十进制(目前不支持)、日期字符串(用于可变长度字符串),字符串 * (为固定长度的字符串) 的_长度_、对象变量、 另一种用户定义的类型,或对象类型Required. Data type of the element; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported),Date, String (for variable-length strings), String * length (for fixed-length strings),Object, Variant, another user-defined type, or an object type.


仅在模块级别,可以使用Type语句。一旦声明了用户定义的类型使用Type语句,可以声明范围内的任意位置的声明该类型的变量。使用模糊私有公共ReDim,或静态声明用户定义的类型的变量。在标准模块类模块中,用户定义的类型是公共的默认值。这种方式查看可以使用Private关键字进行更改。行号行标签类型中不允许使用...结束类型块。用户定义的类型通常用于与数据记录,经常包含多个不同数据类型的相关因素。下面的示例演示使用固定大小数组的用户定义的类型中:The Type statement can be used only at module level. Once you have declared a user-defined type using the Type statement, you can declare a variable of that type anywhere within thescope of the declaration. Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type. In standard modules andclass modules, user-defined types are public by default. This visibility can be changed using the Private keyword. Line numbers andline labels aren't allowed in Type...End Type blocks. User-defined types are often used with data records, which frequently consist of a number of related elements of different data types. The following example shows the use of fixed-size arrays in a user-defined type:

Type StateData 
    CityCode (1 To 100) As Integer    ' Declare a static array. 
    County As String * 30 
End Type 

Dim Washington(1 To 100) As StateData 

在前面的示例中,StateData包括CityCode静态数组和记录Washington具有相同的结构为StateData。固定大小数组的用户定义的类型中声明时,必须用数值文本或常数,而不是变量声明其尺寸。In the preceding example, StateData includes the CityCode static array, and the record Washington has the same structure as StateData. When you declare a fixed-size array within a user-defined type, its dimensions must be declared with numeric literals or constants rather than variables.


此示例使用Type语句来定义用户定义的数据类型。在模块级别仅使用Type语句。如果它显示在类模块中,通过专用关键字前面必须Type语句。This example uses the Type statement to define a user-defined data type. The Type statement is used at the module level only. If it appears in a class module, a Type statement must be preceded by the keyword Private.

Type EmployeeRecord    ' Create user-defined type. 
    ID As Integer    ' Define elements of data type. 
    Name As String * 20 
    Address As String * 30 
    Phone As Long 
    HireDate As Date 
End Type 
Sub CreateRecord() 
    Dim MyRecord As EmployeeRecord    ' Declare variable. 

    ' Assignment to EmployeeRecord variable must occur in a procedure. 
    MyRecord.ID = 12003    ' Assign a value to an element. 
End Sub
© 2018 Microsoft