Valores de variável de objeto (Visual Basic)
A variable of the Tipo de dados Object can refer to data of any type. The value you store in an Object variable is kept elsewhere in memory, while the variable itself holds a pointer to the data.
Object Classifier Functions
Visual Basic supplies functions that return information about what an Object variable refers to, as shown in the following table.
Function |
Returns True if the Object variable refers to |
---|---|
An array of values, rather than a single value |
|
A Tipo de dados Data (Visual Basic) value, or a string that can be interpreted as a date and time value |
|
An object of type DBNull, which represents missing or nonexistent data |
|
An exception object, which derives from Exception |
|
Nada (Visual Basic), that is, no object is currently assigned to the variable |
|
A number, or a string that can be interpreted as a number |
|
A reference type (such as a string, array, delegate, or class type) |
You can use these functions to avoid submitting an invalid value to an operation or a procedure.
TypeOf Operator
You can also use the Operador TypeOf (Visual Basic) to determine whether an object variable currently refers to a specific data type. The TypeOf...Is expression evaluates to True if the run-time type of the operand is derived from or implements the specified type.
The following example uses TypeOf on object variables referring to value and reference types.
' The following statement puts a value type (Integer) in an Object variable.
Dim num As Object = 10
' The following statement puts a reference type (Form) in an Object variable.
Dim frm As Object = New Form()
If TypeOf num Is Long Then Debug.WriteLine("num is Long")
If TypeOf num Is Integer Then Debug.WriteLine("num is Integer")
If TypeOf num Is Short Then Debug.WriteLine("num is Short")
If TypeOf num Is Object Then Debug.WriteLine("num is Object")
If TypeOf frm Is Form Then Debug.WriteLine("frm is Form")
If TypeOf frm Is Label Then Debug.WriteLine("frm is Label")
If TypeOf frm Is Object Then Debug.WriteLine("frm is Object")
The preceding example writes the following lines to the Debug window:
num is Integer
num is Object
frm is Form
frm is Object
The object variable num refers to data of type Integer, and frm refers to an object of class Form.
Object Arrays
You can declare and use an array of Object variables. This is useful when you need to handle a variety of data types and object classes. All the elements in an array must have the same declared data type. Declaring this data type as Object allows you to store objects and class instances alongside other data types in the array.
Consulte também
Tarefas
Como: Fazer referência à instância atual de um objeto (Visual Basic)
Como: Determinar a qual tipo uma variável de objeto se refere (Visual Basic)
Como: Determinar se dois objetos estão relacionados (Visual Basic)
Como: Determinar se dois objetos são idênticos (Visual Basic)
Conceitos
Variáveis de objeto no Visual Basic
Declaração de variável de objeto (Visual Basic)