By now you may understand that windows can have properties and methods added to them. The catch is that a window’s properties and methods belong only to the window to which they have been added. Other windows in the project do not have direct access to these properties and methods.

In this project there are two windows in this project; Window1 and Window2.

Both windows have a PushButton with the caption “Show My Message”. Suppose we wanted a method to execute when a user clicks either of these buttons. We could create a “ShowMyMessage” method for each window.

The methods in each of the two windows would be identical:
Sub ShowMyMessage(s as string)
     msgbox s
End Sub

The code in Window1’s PushButton’s Action event would be:
Sub Action()
     ShowMyMessage(“Window1’s message”)
End Sub

The code in Window2’s PushButton’s Action event would be:
Sub Action()
     ShowMyMessage(“Window2’s message”)
End Sub


Window1      Window2

Now, here’s the point! Each window needed to have its own method to handle its PushButton’s Action event. What if there were more windows? Say 10 windows each with similar “Show My Message” buttons. Each of the 10 windows would need their own method to handle button clicks. Wouldn’t it be nice if we could write just one method that all of the windows could use? Enter the global method.

In the project window near the top of this page you may have noticed an item called “Globals”. This is a Module that has been added to the project using the “New Module” menu item in Xojo’s File Menu. Modules can have properties and methods added to them just as windows can. But, here’s the really good news! Properties and methods added to a module are available to all objects in all windows. We can write one ShowMyMessage method that all of the windows can access. None of the individual windows will need to have their own ShowMyMessage method.

Programmers often like to indicate that a property or method is global by beginning names with a small “g”.


The code in the PushButtons on each window would be as shown above except that “ShowMyMessage” would be replaced with “gShowMyMessage”.

In the picture above a global property has also been added to the Globals module. Like global methods, global properties can also be accessed by any object in any window.

A Word About Scope

Depending on where a property (or method) is created it can have a different “lifetime”. For example, a global property is “alive” as long as the program is running. It is constantly available. A property added to a window, however, is only “alive” as long as its window is open. The shortest “lived” property of all is one created in code by a “dim” statement (Eg. “dimtheName as string“). These properties (variables) are only “alive” while the code that contains them is executing.

The proper term for a property’s or method’s “lifetime” is “scope”. It is generally considered good programming to keep a property or method “alive” only as long as it is needed. This is because when a property’s or method’s “lifetime” runs out, the memory it occupied is freed up and can be used for other things.