Introduction  Instantiation  Events & Event Handlers   Global Methods & Properties 
Messages  Hints,Tips & Code  Links & Glossary  Two Demo Projects  Flow Diagram

INSTANTIATION & PROPERTIES

INSTANTIATION

     An Instance of something is an example of that something, the actual embodiment of it. In RB, that "something" is a Class. A Class is a specification (eg., of a window or of a pushbutton). An Instance of that Class is an actual window or pushbutton on your screen. The specification in the Class is just a set of instructions for creating an Instance. You could write it on a piece of paper. You can't use it (except to create an Instance). You can use an Instance of it (eg., you can display an object in the window, position a pushbutton in that window, give it a caption).

     Consider an analogy. You have won a free holiday in a raffle. The specification of the holiday has the following features:

Cost

Departure Date

Country

Length

Travel

Your Name

£1,000-£1,500

1 to 20 (August)

Brazil, Greece, Borneo

2 weeks

No info

Anything you like

Your Choice -->

£1,350

12th

Borneo

-

-

Hiram J Schikelgruber III


     
Your choices take the following form:

Cost: A number 1,000 through 1,500;
Departure Date: A number 1 through 20;
Country: Choice of 3
Length: No choice;
Travel: No choice;
Your Name: Any piece of text ("Free Text")

     Your holiday then materialises and you enjoy it. The free holiday offered in the raffle is like an object Class in RB (the "Holiday Class" in this analogy). The actual holiday that you went on is like an Instance of that Class, with the choices being Properties, each with its particular data filled in (but within the limits in the Class specification both as to the nature and variety of the Properties allowed and the choices available in each Property). You were not able to have any choice concerning the length of the holiday (although you knew its length) nor in the method of travel (you didn't even know what it will be). Other winners in the raffle went on holidays with other Instances of the holiday Class and with different Values in each of the Properties.

PROPERTIES

     The properties of this holiday object Class have different characteristics (the type numbers below are my own):

     Returning to the world of RB, you can create an Instance of a window or of a Control - a Pushbutton, Edit Field etc. - manually. (By "manually" is meant, in the case of a window, by choosing File > New Window; and in the case of a Control, by dragging it from the Controls Palette on to an open window whose name is listed in the Project window.) You will be able to enter data into its Property window, with choices to be made which will also be of different types. For example, when designing a window, you will see this window (see right, which is the Properties Window for Window1). On it are marked the different types of Property (although Type 5 excludes spaces in the Name of the window). But there are more types for a window:

Type 6: Boolean (True or False);
Type 7: Bring up a file name dialogue or some other             choice (eg., a colour picker);
Type 8: Not available on the Properties Window.

     An example of type 4 is the shape of the window. You have no choice at all, and you will discover that it will be rectilinear - no round etc. windows allowed.

     Type 8 includes the Top and Left properties. These can be Set only in code, in the usual way:




     When you are designing an application in the
IDE you will manually create several objects such as windows with controls in them and change some of the Values in their Properties Windows. These Values are called "Starting Values". When the application runs the objects will appear (when instructed by code) in accordance with those Starting Values. As the application runs, some of those Starting Values could be changed by code (as programmed by you) from time to time. For example, a Static Text's Text Property might change to "All choices now set" and a pushbutton's Caption might be changed to "Finish".

CODE CAN CREATE INSTANCES OF OBJECTS

     Important! Note that any Instance of a Property (Variable) created in the code of a Method will be destroyed when execution of all the code in that Method been has completed. This is because variables so created are Local to that Method. If you want to create a variable that remains in existence for longer, create a Property (which a Variable really is) in a window's Source Code (when it will remain in existence so long as that window is open. It will be available to all the code in that window's Source Code - but only that window). (To create such a Property, open the window's Source Code and choose from RB's main Menu Bar Edit > New Property. It will then appear at the bottom of the Browser for that window's Source Code.) Alternatively, it could be created in the App Class when it will remain in existence so long as the program is running and be available to all the program's code, becoming Global - see details in the Glossary for the App Class). A Module can also be used to contain a Global Variable.

     Instances of some objects created by the programmer manually (or which RB creates by default) are automatically Instantiated immediately the application runs. These are:

The Default Window which opens immediately (RB names it Window1 by default);
Any other Windows created manually by the programmer and whose names and icons will     therefore appear in the Project Window;
Any Control contained in a window;
Menu Items; and
The Application Class Instance (the small green cube icon in the Project window).

     Any other Instances that you want to be created must be explicitly created by code. These Instances are created by the Dim statement. First, the Scalar Instances:

     This code:

Instantiates a variable called "MyIntVar";
Gives it the
Data Type "Integer"; and
Initialises it to zero.

     The situation is different for all other object Instances (ie., non-Scalar objects). Those need to be explicitly Instantiated and explicitly Initialised.

     First, using the Dim statement. For example:

     The first line simply creates a variable called "f" of the Data Type "FolderItem" - nothing more. It does not create an Instance of anything nor does it Assign a Value to it. That Instantiation is done in the second line, and that line also Initialises the Instance by Assigning it a Value. GetFolderItem is actually a Function built into RB which, when Called, creates an Instance of the Class "FolderItem". It then expects to have Assigned to it a Value of Data Type "FolderItem" and the Value of the above file path is of that Type. The Value Assigned to the Instance is "MyHardDisc:ThisFolderName:ThatFileName". Assigning that Value to the Instance Initialises it. Therefore, note that with these non-Scalar Classes, their Instantiation and Initialisation must be done with explicit code, unlike the automatic Instantiation and Initialisation of Scalar Instances which can be done in one line of code (see above).

     I read somewhere that when reading a line which includes "f" (like above), read it as "folderitem", so the line above would be read as "f stands for "GetFolderItem("MyHardDisc:ThisFolderName:ThatFileName")" This is much more understandable.

     A second way to create an Instance of a non-Scalar object is to use the "New" operator. For example:

     The first line creates a variable of Data Type "Window". (If the second version of the code is used, then as Window1 is of the Data Type "Window", so also is w.) The second line creates another Instance of the already-existing Class "Window1". The new window will immediately appear (unless you add "w.Hide"). "w" effectively stands for "the new copy of Window1".

     The new window, however, appears as a copy of Window 1, as you would expect from the second line. This means that the new window will contain all the Controls, Event Handlers, Methods etc. that "Window1" has. This is unfortunate because some programmers might need to write code which will create a new window during the running of the program and which will fill it with Controls specially chosen, by that code, to be suitable for the situation arising at the time which requires the creation of the new window. (By contrast, a custom Message Dialogue Box can be created in code using the MessageDialogBox Class.)

     Therefore it would be nice if it were possible to do:

     But it's not, so there are two limitations here:

     First, it is not possible in RB to create, in code, an Instance of a blank window (it must be an Instance of an existing Window with all its existing Controls);

     Second, even if it were, you cannot create, in code, an Instance of a Control except by creating a copy of a Control that already exists in one of your windows.

     A part way around this is to create a window ("WindowThatIMightNeed") manually in the Project window with a selection of Controls dragged into it. This would be in the hope that they will be the ones needed when the code (which needs the window) is executed and displays the window. That code could also include instructions that repositions, or hides unwanted, Controls (eg., "Pushbutton1.Hide"). But the problem here is that the selection of Controls might not include the ones wanted when the code which needs the window is executed. It might be impossible to guess that. However, you could, in that code, change the properties of the wanted Controls (eg., change the width, position, caption etc.). But this workaround may be no help in many circumstances. On the other hand, perhaps it would be unusual for a programmer to want to have a totally new custom window created.

     I have made more of a meal of this matter concerning windows than in principle should concern a beginner, but the creation of a custom window in code seemed to me to be a reasonable thing to want to do, and the discovery of why it could not be done taught me a lot about how windows and their Controls are different from other RB objects.

Introduction  Instantiation  Events & Event Handlers   Global Methods & Properties 
Messages  Hints,Tips & Code  Links & Glossary  Two Demo Projects  Flow Diagram