Compartilhar via


Tipo de dados Object

Mantém os endereços que se referem a objetos. You can assign any reference type (string, array, class, or interface) to an Object variable. An Object variable can also refer to data of any value type (numeric, Boolean, Char, Date, structure, or enumeration).

Comentários

The Object data type can point to data of any data type, including any object instance your application recognizes. Use Object when you do not know at compile time what data type the variable might point to.

The default value of Object is Nothing (a null reference).

Data Types

You can assign a variable, constant, or expression of any data type to an Object variable. To 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())

The Object data type is a reference type. However, Visual Basic treats an Object variable as a value type when it refers to data of a value type.

Storage

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. 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

  • Considerações de interoperabilidade. Se você está em uma interface com componentes não são escritos para o.NET Framework, para objetos de automação ou COM exemplo, tenha em mente que os tipos de ponteiro em outros ambientes não são compatíveis com o Visual Basic Object tipo.

  • Desempenho. Uma variável declarada com o Object o tipo é flexível o suficiente para conter uma referência a qualquer objeto. 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.

    When you declare an object variable, try to use a specific class type, for example OperatingSystem, instead of the generalized Object type. 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.

  • Alargamento. Ampliar aTudo tipos de dados e todos os tipos de referência para o Objecttipo de dados. This means you can convert any type to Object without encountering a System.OverflowException error.

    However, if you convert between value types and Object, Visual Basic performs operations called boxing and unboxing, which make execution slower.

  • Tipo Caracteres. Object tem nenhum caractere de tipo literal ou identificador tipo de caractere.

  • Framework Type. O tipo correspondente na.NET Framework é o System.Object classe.

Exemplo

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)

Consulte também

Tarefas

Como: Determinar se dois objetos estão relacionados (Visual Basic)

Como: Determinar se dois objetos são idênticos (Visual Basic)

Referência

Resumo de tipo de dados (Visual Basic)

Object

Funções de conversão de tipo (Visual Basic)

Resumo de conversão (Visual Basic)

Conceitos

Uso eficiente de tipos de dados (Visual Basic)

Objeto como o tipo de dados Universal (Visual Basic)