UserGuide

Me vs. Self

From Xojo Documentation

When you add a control such as a PushButton to a Window, 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 Window itself (or WebPage or iOSView). You can use the Self prefix to be explicit that you are referring to the Window (or WebPage or iOSView) 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 Window/WebPage/iOSView (such as PushButton.Action), you must use the Me prefix.

For example, consider the Visible property which is on both a PushButton 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 Action 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 Window/WebPage/iOSView (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 Windows/WebPages/iOSViews.

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 Window/WebPage/iOSView.

See Also

Window, WebPage, iOSView classes; Me, Self commands