Object Data TypeObject Data Type

保存引用对象的地址。Holds addresses that refer to objects. 可以将任何引用类型(字符串、数组、类或接口)分配给 Object 变量。You can assign any reference type (string, array, class, or interface) to an Object variable. Object变量还可以引用任意值类型的数据(数值、、 Boolean CharDate 、结构或枚举)。An Object variable can also refer to data of any value type (numeric, Boolean, Char, Date, structure, or enumeration).

备注Remarks

Object数据类型可以指向任何数据类型的数据,包括应用程序识别的任何对象实例。The Object data type can point to data of any data type, including any object instance your application recognizes. Object 编译时不知道变量可能指向的数据类型时,请使用。Use Object when you do not know at compile time what data type the variable might point to.

的默认值 ObjectNothing (空引用)。The default value of Object is Nothing (a null reference).

数据类型Data Types

可以将任何数据类型的变量、常量或表达式分配给 Object 变量。You can assign a variable, constant, or expression of any data type to an Object variable. 若要确定 Object 变量当前引用的数据类型,可以使用 GetTypeCode 类的方法 System.TypeTo determine the data type an Object variable currently refers to, you can use the GetTypeCode method of the System.Type class. 下面的示例对此进行了演示。The following example illustrates this.

Dim myObject As Object
' Suppose myObject has now had something assigned to it.
Dim datTyp As Integer
datTyp = Type.GetTypeCode(myObject.GetType())

Object数据类型为引用类型。The Object data type is a reference type. 但是, Object 当变量引用值类型的数据时,Visual Basic 将变量视为值类型。However, Visual Basic treats an Object variable as a value type when it refers to data of a value type.

存储Storage

无论它引用哪种数据类型, Object 变量都不包含数据值本身,而是指向值的指针。Whatever data type it refers to, an Object variable does not contain the data value itself, but rather a pointer to the value. 它在计算机内存中始终使用四个字节,但这不包括表示变量值的数据的存储。It always uses four bytes in computer memory, but this does not include the storage for the data representing the value of the variable. 由于使用指针查找数据的代码, Object 保存值类型的变量比显式类型化变量略慢。Because of the code that uses the pointer to locate the data, Object variables holding value types are slightly slower to access than explicitly typed variables.

编程提示Programming Tips

  • 互操作注意事项。Interop Considerations. 如果与不是为 .NET Framework 编写的组件(如自动化或 COM 对象)交互,请记住,其他环境中的指针类型与 Visual Basic 类型不兼容 ObjectIf you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, keep in mind that pointer types in other environments are not compatible with the Visual Basic Object type.

  • 性能。Performance. 用类型声明的变量的 Object 灵活性足以包含对任何对象的引用。A variable you declare with the Object type is flexible enough to contain a reference to any object. 但是,在此类变量上调用方法或属性时,始终会产生后期绑定(在运行时)。However, when you invoke a method or property on such a variable, you always incur late binding (at run time). 若要强制进行早期绑定(在编译时)和更好的性能,请使用特定类名称声明变量,或将其转换为特定数据类型。To force early binding (at compile time) and better performance, declare the variable with a specific class name, or cast it to the specific data type.

    声明对象变量时,请尝试使用特定类类型, OperatingSystem 而不是通用 Object 类型。When you declare an object variable, try to use a specific class type, for example OperatingSystem, instead of the generalized Object type. 还应使用最具体的类,如 TextBox 而不是 Control ,以便您可以访问其属性和方法。You should also use the most specific class available, such as TextBox instead of Control, so that you can access its properties and methods. 通常可以使用对象浏览器中的 "" 列表来查找可用的类名。You can usually use the Classes list in the Object Browser to find available class names.

  • 扩大.Widening. 所有数据类型和所有引用类型都扩大到 Object 数据类型。All data types and all reference types widen to the Object data type. 这意味着,可以将任何类型转换为, Object 而不会遇到 System.OverflowException 错误。This means you can convert any type to Object without encountering a System.OverflowException error.

    但是,如果在值类型和之间 Object 进行转换,Visual Basic 将执行称为装箱取消装箱的操作,这会使执行速度变慢。However, if you convert between value types and Object, Visual Basic performs operations called boxing and unboxing, which make execution slower.

  • 键入字符。Type Characters. Object没有文本类型字符或标识符类型字符。Object has no literal type character or identifier type character.

  • Framework 类型。Framework Type. .NET Framework 中的相应类型是 System.Object 类。The corresponding type in the .NET Framework is the System.Object class.

示例Example

下面的示例演示 Object 指向对象实例的变量。The following example illustrates an Object variable pointing to an object instance.

Dim objDb As Object
Dim myCollection As New Collection()
' Suppose myCollection has now been populated.
objDb = myCollection.Item(1)

另请参阅See also