BBj Custom Objects Tutorial: Program #1 - Writing a Check

Example Programs

Here are some example BBj programs. Each example is designed to demonstrate some aspect of using Custom Object Classes in BBj.

The complete source code for each program is included in an appropriately named sub-folder in the zip file you can download here.

Program #1: Writing a Check

Let’s take a look at a simple BBj program that will create an instance of a Check class and then use that class’s methods to demonstrate basic class use. Here is the code from WriteACheck.src:

Program 1. Writing a Check with WriteACheck.src

As the developer, we need to define the Check class, which we do in these lines:

Program 1-1. Defining the Check Class

Once the Check class has been defined, we declare a Check variable, create an instance with the constructor that takes two parameters, and assign that instance to our Check variable. We print out the results to demonstrate our success. All of this happens in this code:

Program 1-2. Instantiating a Check with the Desired Arguments

Finally, we change the payee of the Check variable, and then try to change the amount, printing out the results after each change. All of this happens here:

Program 1-3. Modifying the Check Through Its Methods

Execution

Running Program 1 resulted in the following SysConsole output:

Figure 1. The Output from Program 1

Observations

There are some details worth pointing out about this code.

By writing an explicit setAmount(BBjNumber newAmount) setter (and not allowing BBj to provide an implicit one), we were able to control when a user can and when a user cannot set the Check Amount.

The definition of a class can be directly inline in the code (as it is here). It can precede the code that uses the class, or it can follow it. It can also be in a completely different file (by including a USE Verb).

Your code will be easier to maintain, and more readable, if you do not put more than one class definition in a source file. Separate any code that uses that class from the class definition itself, including any sequential code.

Class method and field names are case sensitive, although traditional BBx® variable names are case insensitive. In the statement #Payee$ = newPayee$, the field reference #Payee$ is case-sensitive, but newPayee$ (because it is a traditional BBx string variable) is not case-sensitive.

Class field and parameter names follow standard BBj type naming rules:

BBjString field and parameter names follow string variable rules (x$ or x!).

BBjInt field and parameter names follow integer variable rules (x% or x!).

BBjNumber field and parameter names follow numeric variable rules (x or x!).

All other field and parameter names follow object rules (x!).

The types of all fields are defined in their field statements.

The types of all method parameters are defined in their method statements.

By using the declare statement, you can also define the class type of local variables. When a class type has been defined for a variable, BBj will enforce type safety for that variable.

Next Step: Example Program #2

BBj Custom Objects Tutorial Contents

BBj Custom Objects Tutorial: Introduction

BBj Custom Objects Tutorial: Interfaces

BBj Custom Objects Tutorial: Classes

BBj Custom Objects Tutorial: Fields

BBj Custom Objects Tutorial: Methods

BBj Custom Objects Tutorial: Using Custom Objects

BBj Custom Objects Tutorial: Program #1 - Writing a Check

BBj Custom Objects Tutorial: Program #2 - Protected and Private Fields

BBj Custom Objects Tutorial: Program #3: The Static Keyword

BBj Custom Objects Tutorial: Program #4 - Error Handling

BBj Custom Objects Tutorial: Program #5 - Class Inheritance

BBj Custom Objects Tutorial: Program #6 - Callback Choices

BBj Custom Objects Tutorial: Program #7 - Constructors and Field Initialization