Object Data TypeObject Data Type
Include indirizzi che fanno riferimento a oggetti.Holds addresses that refer to objects. È possibile assegnare qualsiasi tipo di riferimento (stringa, matrice, classe o interfaccia) a una Object
variabile.You can assign any reference type (string, array, class, or interface) to an Object
variable. Una Object
variabile può anche fare riferimento a dati di qualsiasi tipo di valore (numeric,,,, Boolean
Char
Date
Structure o Enumeration).An Object
variable can also refer to data of any value type (numeric, Boolean
, Char
, Date
, structure, or enumeration).
CommentiRemarks
Il Object
tipo di dati può puntare a dati di qualsiasi tipo di dati, inclusa qualsiasi istanza di oggetto riconosciuta dall'applicazione.The Object
data type can point to data of any data type, including any object instance your application recognizes. Usare Object
quando non si conosce in fase di compilazione a quale tipo di dati può puntare la variabile.Use Object
when you do not know at compile time what data type the variable might point to.
Il valore predefinito di Object
è Nothing
(un riferimento null).The default value of Object
is Nothing
(a null reference).
Tipi di datiData Types
È possibile assegnare una variabile, una costante o un'espressione di qualsiasi tipo di dati a una Object
variabile.You can assign a variable, constant, or expression of any data type to an Object
variable. Per determinare il tipo di dati Object
a cui fa attualmente riferimento una variabile, è possibile usare il GetTypeCode metodo della System.Type classe.To determine the data type an Object
variable currently refers to, you can use the GetTypeCode method of the System.Type class. Questa condizione è illustrata nell'esempio seguente.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())
Il Object
tipo di dati è un tipo di riferimento.The Object
data type is a reference type. Tuttavia, Visual Basic considera una Object
variabile come tipo di valore quando fa riferimento ai dati di un tipo di valore.However, Visual Basic treats an Object
variable as a value type when it refers to data of a value type.
ArchiviazioneStorage
Indipendentemente dal tipo di dati a cui fa riferimento, una Object
variabile non contiene il valore di dati stesso, bensì un puntatore al valore.Whatever data type it refers to, an Object
variable does not contain the data value itself, but rather a pointer to the value. Usa sempre quattro byte nella memoria del computer, ma non include l'archiviazione per i dati che rappresentano il valore della variabile.It always uses four bytes in computer memory, but this does not include the storage for the data representing the value of the variable. A causa del codice che usa il puntatore per individuare i dati, le Object
variabili che mantengono i tipi di valore sono leggermente più lente per accedere alle variabili tipizzate in modo esplicito.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.
Suggerimenti per la programmazioneProgramming Tips
Considerazioni sull'interoperabilità.Interop Considerations. Se si è connessi con componenti non scritti per il .NET Framework, ad esempio oggetti COM o di automazione, tenere presente che i tipi di puntatore in altri ambienti non sono compatibili con il
Object
tipo di Visual Basic.If 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 BasicObject
type.Prestazioni.Performance. Una variabile dichiarata con il
Object
tipo è sufficientemente flessibile da contenere un riferimento a qualsiasi oggetto.A variable you declare with theObject
type is flexible enough to contain a reference to any object. Tuttavia, quando si richiama un metodo o una proprietà in una variabile di questo tipo, si verifica sempre un' associazione tardiva (in fase di esecuzione).However, when you invoke a method or property on such a variable, you always incur late binding (at run time). Per forzare l' associazione anticipata (in fase di compilazione) e migliorare le prestazioni, dichiarare la variabile con un nome di classe specifico oppure eseguirne il cast al tipo di dati specifico.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.Quando si dichiara una variabile oggetto, provare a usare un tipo di classe specifico, ad esempio OperatingSystem , anziché il tipo generalizzato
Object
.When you declare an object variable, try to use a specific class type, for example OperatingSystem, instead of the generalizedObject
type. È inoltre consigliabile utilizzare la classe più specifica disponibile, ad esempio TextBox anziché Control , in modo da poter accedere alle proprietà e ai metodi.You should also use the most specific class available, such as TextBox instead of Control, so that you can access its properties and methods. In genere, è possibile usare l'elenco delle classi nella Visualizzatore oggetti per trovare i nomi delle classi disponibili.You can usually use the Classes list in the Object Browser to find available class names.Conversioni.Widening. Tutti i tipi di dati e tutti i tipi di riferimento vengono ampliati al
Object
tipo di dati.All data types and all reference types widen to theObject
data type. Ciò significa che è possibile convertire qualsiasi tipo inObject
senza riscontrare un System.OverflowException errore.This means you can convert any type toObject
without encountering a System.OverflowException error.Tuttavia, se si esegue la conversione tra tipi di valore e
Object
, Visual Basic esegue operazioni unboxingdenominate Boxing e unboxing, che rendono l'esecuzione più lenta.However, if you convert between value types andObject
, Visual Basic performs operations called boxing and unboxing, which make execution slower.Digitare i caratteri.Type Characters.
Object
non ha un carattere di tipo letterale o un carattere di tipo identificatore.Object
has no literal type character or identifier type character.Tipo di framework.Framework Type. Il tipo corrispondente nella .NET Framework è la System.Object classe.The corresponding type in the .NET Framework is the System.Object class.
EsempioExample
Nell'esempio seguente viene illustrata una Object
variabile che punta a un'istanza dell'oggetto.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)
Vedere ancheSee also
- Object
- Tipi di datiData Types
- CStringType Conversion Functions
- Riepilogo della conversioneConversion Summary
- Uso efficiente dei tipi di datiEfficient Use of Data Types
- Procedura: determinare se due oggetti sono correlatiHow to: Determine Whether Two Objects Are Related
- Procedura: determinare se due oggetti sono identiciHow to: Determine Whether Two Objects Are Identical