When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Technical Support Partner Central

API/Plug-in Center

Tutorials

In our previous tutorial, we looked at the basics of how to create a plug-in and use simple API commands. Our next step will be to look at the use of constants in a plug-in. There are probably several developers out there who at start-up, call a function usually named Compiler or something along those lines that holds all the variable definitions. Some of these variables may in fact be used as a constant inside the database. If you find that you are creating the same constants over and over, why not move them into a plug-in? In addition, when you are writing a plug-in your constants may be needed for your plug-in to function properly, or perhaps you want to create a plug-in that simply holds a series of constants. In our example below, we will be showing how to go about creating a plug-in that will hold several kinds of constants.

If you haven't already taken a look at the first tutorial, you should do so now since we may be discussing procedures that were covered there.

Tutorial 1

Start up the Plug-In Wizard and create a new Project. The first thing we want to do is click on the "Edit Constants..." button. This will display a dialog as shown below:

screenshot

This dialog contains four buttons: Add Group, Add Constant, Delete, and Close.

Add Group is very similar to the Add Theme button. Add Group allows you to group your constants together. You must have at least one Group before you can start creating your constants.

Add Constants will bring up a Constants dialog, which we will explain in a bit. This dialog is where your Constant is defined.

The Delete button will either delete a Group or Constant depending on which item is highlighted to the left in the Group/Constants listing.

The Close button simply closes the "Edit Constants" dialog.

Creating a Group

Let's go ahead and create our first group for our constants. Click on the "Add Group..." button and a dialog will be displayed asking for the name of the Group, just like the "Add Themes" button.

screenshot

Here we are naming our Group "Tutorial Constants." After clicking the "OK" button you should now see that the Constant's listing contains the name of our new Group.

screenshot

Creating a Constant

We can now proceed with creating a Constant. Let's now click on the "Add Constant..." button. This will display the following dialog:

screenshot

Within this dialog is where your Constant is declared. There are three areas of importance, the Name, Type, and Value.

The Name is simply the name of the Constant. The Type is the type of Constant whether it is a string, real, or longint value. The Value holds the value of the new Constant.

Our first Constant will be a real, which will hold the value of Pi out to a few decimal places. When finished the Constant's dialog should look like the following:

screenshot

If you wish, you can now go ahead and define several more Constants. We have created two more: Tutorial_4DName which is a string that holds "4th Dimension", and Tutorial_CurrentYear which is a longint that holds "2001".

Constant in Resource or in Header File

The final question to ask is, do you want the Constants to be held in the Resource file or in the 4DPlugin.h header file? The option is totally up to you. By default, the Constants are held in the Resource file, but if you wish to move them to the Header file, make sure the "Define Constants in 4Dplugin.h" checkbox is marked. Doing so will move the Constants into the header file. If you click on the 4DPlugin.h tab you should see the following listed if you have chosen the header route:

// --- Tutorial Constants
#define Tutorial_Pi 3.1415
#define Tutorial_4DName "4th Dimension"
#define Tutorial_CurrentYear 2001

Generating the Constants Plug-In

You are now ready to generate your code to disk. You can compile your plug-in with only a series of constants and then use this plug-in in your databases. It is not necessary to put any code into the 4Dplugin.c file. After compiling your plug-in, you can now drop it into the Mac/Win 4DX folder of your choice, and when you start up the database the new constants will be available from the Constants tab of the Explorer.

Constants Sample Database

We have provided a sample database that uses the Constants we defined above. If you go to the Runtime Environment and choose "4D Constants..." A dialog will be opened where three buttons will display the contents of the three constants we have created. In the Design Environment, the scripts of the three buttons are open along with the Dialog that holds the buttons.

Downloads

Tutorial 2 Source Files (Mac)
Tutorial 2 Source Files (Windows)



International | Company | Contact 4D | Site Map | Privacy Policy | © 4D UK 1995-2008 | Change font size: [A] [A] [A] | Print this page