![]() | ||
Introduction Instantiation Events & Event Handlers Global Methods & Properties | ||
GLOBAL METHODS & PROPERTIES | ||
Where to put Global Methods and Global Properties In this discussion it is important to distinguish between code which is contained within the Declaration of a Method, (ie, the Method's code written by you) and the code somewhere in your program which Calls the Method (the code that you write which has the effect "Execute the code in the Method"). If a Method or Property (and a Property can act as a Variable like a variable in any computer language) is Declared (that is, specified) in the Source Code of a window, it can only be Referred to ("seen by") other code or objects that are also in the Source Code of that window. In other words it will be Local to that window. This is usually an advantage, because it is good programming practice to limit the Scope of such things to the part of an RB program where it is to be used (eg., in its parent window). This prevents a piece of code (outside the Scope of the method or Property) Referring to that method or Property "by mistake". For example, the programmer might be tempted to make a Call to the Method or to Refer to the Property in a source code outside the intended Scope of the Method or Property. This would result in a Compile error ("no such method") which would bring the fault immediately to the attention of the programmer, so preventing confusing results difficult to debug. However, some Methods and Properties need to be available to all parts of the program and therefore have to be put in a highly "visible" place (like a television mast must be placed on a hill). An example is a File Save method which could be Called from a Menu Item or from the Source Code of several windows or from the Event Handler of a Control. You do not want to make several copies of this code to be located in each of these places. Amendments would be time-consuming and risk error. There are two such places to locate Global Methods and Properties: the App Class and a Module. (Modules are not included in this site - please see "Modules" in the index of the manual.) The App Class To create a Method in the App Class (or anywhere else) look in the manual under "methods, adding to windows" in the index (which gives pages 223-228 in the manual for Version 5.5). The steps to take to actually create a Property are the same wherever the Property will be located. Some extra details for creating one in the Application Class are listed in the index under "Application Class, methods of, p 387". To use the App Class to contain a Method, simply double-click on the little green cubic icon in the RB Project window (see image on the right). The App Source Code and Browser window will open looking exactly like the one for a window. Then create a New Method in the usual way described above. Let's name it "MyGlobalMethod". Now, a Call to "MyGlobalMethod" can be made from anywhere in the program and its name will be recognised and its code will be executed. The Call would just be the name of the Method. Eg.:
You could decide not to have a centrally-located "MyGlobalMethod". You could decide to replace each of these Calls to "MyGlobalMethod" by the actual code that was in "MyGlobalMethod". However, where that code is many lines long, that would be time-consuming, and any amendments would need to be repeated in each of the places, risking error. To create a Property in the App Class (or anywhere else) look in the manual under "Adding Properties to windows" in the Contents Page (which gives page 217 in the manual for Version 5.5). The steps to take to actually create a Property are the same wherever the Property will be located. Some extra details for creating one in the Application Class are listed in the index under "Application Class, methods of, p 387". This will result in the creation of a Variable, which is a version of the term "Property". To use the App Class to contain a Property (Variable), simply double-click on the little green cubic icon in the RB Project window (see image above). The App Source Code and Browser Window will open looking exactly like the one for a window. Then create a new Property (Variable) in the usual way described above. Its Data Type could be many Types but at beginner level keep to Scalar Types which are numbers: String, Boolean and Color (remembering to spell Colour in the American way). Let's Name it "MyGlobalProperty" (or, if you want "MyGlobalVariable"). This Property would be acting like a Variable - a place to store the Value of something that would change during the running of the program, like a counter, the number of windows open, etc. Now, any part of the program can get the current Value of the Property or can change that Value by code such as:
(Don't forget to precede the left hand example with the additional line: Dim X As <the same Data Type as "MyGlobalProperty>") The Value of the Property (Variable) will be automatically Initialised to zero (or to the empty String in the case of a String or to black in the case of a Color) when the program is launched. However, you could cause the Value of "MyGlobalProperty" to be Initialised to some other Value when the program is launched by placing some code in the Open Event Handler of the App Class, such as:
Like other "Open" Events, the "Open" Event of the App Class is triggered when the App Class is Instantiated. This Instantiation is done at launch time. Fortunately, "MyGlobalProperty" will be created before the Assignment to it of its Value (otherwise there would be a "this method or property does not exist" error at the time of compiling). Remember that any Value that you Assign to a Property must be of the same Data Type (Integer, String, FolderItem, Date, etc.) that was specified when the Property was created. Also, note the point above about including "App." | ||
Introduction Instantiation Events & Event Handlers Global Methods & Properties | ||