物件變數指派 (Visual Basic)Object Variable Assignment (Visual Basic)

您可以使用一般指派語句, 將物件指派給物件變數。You use a normal assignment statement to assign an object to an object variable. 您可以指派物件運算式或不使用Nothing關鍵字, 如下列範例所示。You can assign an object expression or the Nothing keyword, as the following example illustrates.

Dim thisObject As Object
' The following statement assigns an object reference.
thisObject = Form1
' The following statement discontinues association with any object.
thisObject = Nothing

Nothing表示目前沒有任何物件指派給該變數。Nothing means there is no object currently assigned to the variable.

初始化Initialization

當您的程式碼開始執行時, 您的物件Nothing變數會初始化為。When your code begins running, your object variables are initialized to Nothing. 其宣告包含初始化的使用者會重新初始化為您在宣告語句執行時所指定的值。Those whose declarations include initialization are reinitialized to the values you specify when the declaration statements are executed.

您可以使用New關鍵字, 在宣告中包含初始化。You can include initialization in your declaration by using the New keyword. 下列宣告語句會宣告物件變數testUri , ver並將特定物件指派給它們。The following declaration statements declare object variables testUri and ver and assign specific objects to them. 每個都會使用適當類別的其中一個多載的函式來初始化物件。Each uses one of the overloaded constructors of the appropriate class to initialize the object.

Dim testUri As New System.Uri("https://www.microsoft.com")
Dim ver As New System.Version(6, 1, 0)

解除Disassociation

設定物件變數以Nothing中止變數與任何特定物件的關聯。Setting an object variable to Nothing discontinues the association of the variable with any specific object. 這可防止您藉由變更變數而不小心變更物件。This prevents you from accidentally changing the object by changing the variable. 它也可讓您測試物件變數是否指向有效的物件, 如下列範例所示。It also allows you to test whether the object variable points to a valid object, as the following example shows.

If otherObject IsNot Nothing Then
    ' otherObject refers to a valid object, so your code can use it.
End If

如果您的變數所參考的物件位於另一個應用程式中, 則此測試無法判斷該應用程式是否已終止, 或只使物件失效。If the object your variable refers to is in another application, this test cannot determine whether that application has terminated or just invalidated the object.

值為的Nothing物件變數也稱為null 參考An object variable with a value of Nothing is also called a null reference.

目前的實例Current Instance

目前物件的實例就是程式碼執行所在的實例The current instance of an object is the one in which the code is currently executing. 由於所有程式碼都是在程式中執行, 因此目前的實例就是叫用此程式的其中一個。Since all code executes inside a procedure, the current instance is the one in which the procedure was invoked.

Me關鍵字會作為參考目前實例的物件變數。The Me keyword acts as an object variable referring to the current instance. 如果程式不是共用的, 它可以使用Me關鍵字來取得目前實例的指標。If a procedure is not Shared, it can use the Me keyword to obtain a pointer to the current instance. 共用程式無法與類別的特定實例建立關聯。Shared procedures cannot be associated with a specific instance of a class.

使用Me特別適合用來將目前的實例傳遞至另一個模組中的程式。Using Me is particularly useful for passing the current instance to a procedure in another module. 例如, 假設您有多個 XML 檔, 而且想要在其中新增一些標準文字。For example, suppose you have a number of XML documents and wish to add some standard text to all of them. 下列範例會定義執行這項操作的程式。The following example defines a procedure to do this.

Sub addStandardText(XmlDoc As System.Xml.XmlDocument)
    XmlDoc.CreateTextNode("This text goes into every XML document.")
End Sub

然後, 每個 XML 檔物件都可以呼叫程式, 並將其目前的實例當做引數傳遞。Every XML document object could then call the procedure and pass its current instance as an argument. 下列範例為其示範。The following example demonstrates this.

addStandardText(Me)

另請參閱See also