Me vs. Self

When you add a control such as a DesktopButton to a DesktopWindow, an instance of the control is created for you and added to the Window. Code that is in the event handlers of a control added to the Window can refer to both its own properties and methods as well as the properties and methods of the window.

By default, all code without a prefix refers to the properties and method of the DesktopWindow itself (or WebPage or MobileScreen). You can use the Self prefix to be explicit that you are referring to the DesktopWindow (or WebPage or MobileScreen) or you can leave it off.

If you want to access a property or method of the control from the code in one of its event handlers on the DesktopWindow/WebPage/MobileScreen (such as Button.Pressed), you must use the Me prefix.

For example, consider the Visible property which is on both a Button and the Window itself. If you want to make the button invisible when it is clicked, you have to make sure you use the proper Visible property in the Pressed event handler. This code:

Visible = False ' Hides window

will hide the window and not the button because the Visible property defaults to Self, which is the window.

To access the Button's Visible property, do this:

Me.Visible = False ' Hides button

To be even clearer, you can use the Self keyword to specifically state that you want to use the Window property. To hide the window, you could instead write:

Self.Visible = False ' Hides window

Note that you might be tempted to refer to the window name directly and write code such as this:

Window1.Visible = False

Do not do this! Should you rename your window, this code will cause a compile error because the name is no longer valid.

If you use the Me prefix outside of a control's event handler on a DesktopWindow/WebPage/MobileScreen (such as in a method), then Me will work the same as Self. To prevent confusion, you should refrain from using the Me prefix outside of control event handlers on DesktopWindows/WebPages/MobileScreens.

When working with classes and subclasses you have added to your project, you should always use Self (or nothing since Self is the default). This is true even if you create a control subclass and are implementing its event handlers. Again, you only use Me when in the event handler for a control (or class) that is added to a DesktopWindow/WebPage/MobileScreen.

See also

DesktopWindow, WebPage, MobileScreen classes; Me, Self commands