Objects, Understanding Properties and Methods
Objects are the fundamental building blocks of Visual Basic. Virtually everything you do in Visual Basic involves modification of objects. Every FlexPro element (databases, diagrams, tables, documents, data sets, etc.) can be represented by a Visual Basic object.
What are Objects and Collections?
An object represents a FlexPro element, such as a database, a diagram, or an individual curve in a diagram. A collection is an object containing several other objects that are usually of the same kind. All the curves in a diagram, for example, are contained in a single collection object. Using properties and methods, you can modify an individual object or a whole collection of objects.
What is a Property?
A property is an attribute of an object or an aspect of its behavior. For example, properties of a diagram include its name, the appearance of the division grid and whether or not the cursors are enabled. To change the characteristics of an object, you need to change the values of its properties.
To specify the value of a property, first reference the object, then enter a dot, the property name, an equals sign and the new value of the property. In the following example, a new comment is assigned to the root folder of the MyDatabase database.
Databases("MyDatabase.fpd").RootFolder.Comments = "My Database"
In this example, Databases references the collection of open databases. The name MyDatabase.fpd identifies a single database in the collection. The Comments property specifically applies to the root folder of this database.
Some properties cannot be set. The help topic for a property specifies whether this property can be set (read/write access) or whether it can only be read (read-only access).
You can obtain information about an object by calling the value of one of its properties so that it will be returned. In the following example, the name of the active database is returned.
databaseName = ActiveDatabase.Name
In this example, ActiveDatabase references the database in the active window in FlexPro. The name of this database is assigned to the variable databaseName.
Note: The help topic of a property specifies whether you can set this property (write-read access), only read it (read-only access) or only write to it (write-only access). The Object Browser in the Visual Basic-Editor also displays the read/write status at the bottom of the browser window when the property is selected.
What is a Method?
A method is an action that can be executed by an object. For example, since a FlexPro document can be printed, the Document object also features a PrintOut method. Methods often have arguments that more clearly define the way the action is to be executed. In the following example, the first three pages of the active document are printed.
ActiveDatabase.ActiveObject.PrintOut FirstPage:=1, LastPage:=3
In most cases, methods are actions and properties are characteristics. The use of a method means that something happens to an object, whereas the use of a property returns information on the object or causes an attribute of the object to be changed.
Returning an Object
Most objects are returned by returning an individual object from a collection. The Databases collection, for example, contains the open FlexPro databases. Use the Databases property of the Application object (the topmost object in the FlexPro object hierarchy) to return the Databases collection.
Once you have accessed the collection, you can return an individual object by specifying an index value in brackets (similar to working with arrays). The index value is usually a number or a name.
In the following example, the Databases property is used to access the Databases collection. The index number is used to return the first database in the Databases collection. The Close method is then applied to the Database object to close the first database in the Databases collection.
Databases(1).Close
In the following example, a name (specified as a string) is used to identify a Database object within the Databases collection.
Databases("Report.fpd").Close
Collection objects often have methods and properties which can be used to modify the complete collection of objects. The Databases object has a Save method that saves all documents in the collection. In the following example, the open documents are saved using the Save method.
Databases.Save
The Database object also features a Save method for saving an individual database. In the following example, the Report.fpd database is saved.
Databases("Report.fpd").Save
If an object located further down in the FlexPro object model is to be returned, you have to "drill down" to this object by using properties and methods that return objects.
To see how this works, open the Visual Basic Editor and in the View menu click on Object Browser. In the Classes list on the left, click on Application. In the members list on the right, click on ActiveDatabase. The text at the bottom of the Object Browser shows that ActiveDatabase is a read-only property that returns a Database object. At the bottom of the Object Browser, click on Database. The Database object is then automatically selected in the Classes list and the Members list shows the members of the Database object. Scroll through the list of members until you find Close. Click on the Close method. The text at the bottom of the Object Browser window shows the syntax for the method. For more information about this method, press F1 or click on the Help button and navigate to the help topic covering the Close method.
Leveraging this information, you can write the following statement for closing the active database.
ActiveDatabase.Close SaveChanges:=fpSaveChanges
In the following example, the caption of the active window is changed.
ActiveDatabase.ActiveWindow.Caption = "My Window"
The ActiveWindow property returns a Window object that displays the active window. Use the Caption property to specify the title of the window.
In the following example, a new database is created and the Save As dialog box appears so that the database can be given a name.
Databases.Add.Save
The Databases property returns a Databases collection. The Add method creates a new database and returns a Database object. The Save method is then applied to the Database object.
As you can see, methods and properties are used to drill down to an object. This means that you return an object by applying a method to a higher-level object in the object hierarchy or accessing a property. Once you have returned the required object, you can use the methods of this object and control its properties. For an overview of the object hierarchy, go to FlexPro Object Model.
How to Get Help on Objects, Methods and Properties
Until you are familiar with the FlexPro Object Model, you can use a few resources that will help you to drill down through the hierarchy.
•Listing Members automatically. Typing a period (.) after an object in the Visual Basic Editor displays a list of the available properties and methods. If, for example, you enter Application., a drop-down list of the methods and properties of the Application object appears.
•Help. You can also use Help to find out which properties and methods can be used for an object. Every object topic in Help contains properties and methods sections, which list the properties and methods for the object. In the Object Browser or in a module, press F1 to get to the corresponding help topic.
•FlexPro Object Model. This topic explains how FlexPro objects are arranged in the hierarchy. Click on an object in the image to display the corresponding help topic.
•Object Browser. The Object Browser in the Visual Basic Editor displays the members (properties and methods) of the FlexPro objects.