Visual Basic 中的 Me、My、MyBase 和 MyClassMe, My, MyBase, and MyClass in Visual Basic

Visual Basic 中的 MeMyMyBaseMyClass 具有類似的名稱,但有不同的用途。Me, My, MyBase, and MyClass in Visual Basic have similar names, but different purposes. 本主題將說明每個實體,以便加以區別。This topic describes each of these entities in order to distinguish them.


Me 關鍵字提供了一種方法,可參考程式碼目前執行所在之類別或結構的特定實例。The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing. Me 的行為就像是參考目前實例的物件變數或結構變數。Me behaves like either an object variable or a structure variable referring to the current instance. 使用 Me 特別適合用來將目前正在執行之類別或結構實例的相關資訊傳遞至另一個類別、結構或模組中的程式。Using Me is particularly useful for passing information about the currently executing instance of a class or structure to a procedure in another class, structure, or module.

例如,假設您在模組中有下列程式。For example, suppose you have the following procedure in a module.

Sub ChangeFormColor(FormName As Form)  
   FormName.BackColor = Color.FromArgb(Rnd() * 256, Rnd() * 256, Rnd() * 256)  
End Sub  

您可以呼叫此程式,並使用下列語句,將 Form 類別的目前實例當做引數傳遞。You can call this procedure and pass the current instance of the Form class as an argument by using the following statement.



My 功能可讓您輕鬆且直覺地存取許多 .NET Framework 類別,讓 Visual Basic 的使用者能夠與電腦、應用程式、設定、資源等進行互動。The My feature provides easy and intuitive access to a number of .NET Framework classes, enabling the Visual Basic user to interact with the computer, application, settings, resources, and so on.


MyBase 關鍵字的行為就像是參考類別目前實例之基類的物件變數。The MyBase keyword behaves like an object variable referring to the base class of the current instance of a class. MyBase 通常用來存取在衍生類別中覆寫或遮蔽的基類成員。MyBase is commonly used to access base class members that are overridden or shadowed in a derived class. MyBase.New 是用來從衍生類別的函式明確呼叫基類的「自類別」(class)。MyBase.New is used to explicitly call a base class constructor from a derived class constructor.


MyClass 關鍵字的行為就像是物件變數,其參考原本實作為之類別的目前實例。The MyClass keyword behaves like an object variable referring to the current instance of a class as originally implemented. MyClass 類似 Me,但會將其上的所有方法呼叫視為已 NotOverridable方法。MyClass is similar to Me, but all method calls on it are treated as if the method were NotOverridable.

另請參閱See also