Skip to content

Me vs. Self

When you add a control such as a PushButton to a Window (or a WebButton to a WebPage or an iOSButton to an iOSView), an instance of the control is created for you and added to the layout. Code that is in the event handlers of a control added in this way 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 owner itself (the Window, WebPage or iOSView). You can use the Self prefix to be explicit that you are referring to the owner (Window/WebPage/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 (not good UI, obviously, but this is just a example), you have to make sure you use the proper Visible property in the Action event handler. This code:

Visible = False // Hides window

hides 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 are using the Window property. So to hide the window, you could instead do this:

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! If you rename your window, this code will cause a compile error because the window 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 need to use Me when in the event handler for a control (or class) that is added to a Window/WebPage/iOSView (as well as  a ContainerControl).