BBj Custom Objects Tutorial: Using Custom Objects

In order for the interpreter to take advantage of custom objects, it is extremely important for the interpreter to know each variable’s type. Clearly, if you have a variable that is a custom object of the class Book, you will use it completely differently than if it is a custom object of the class Auto. It will have different fields, and it will offer different methods you can invoke.

Declaring a Variable’s Type

BBj offers the declare verb to help. Using declare tells the interpreter what type of object a particular variable can hold. Although not strictly required by the language (most of the Business BASIC language syntax existed long before BBj and custom objects were developed), BASIS recommends that you declare all of your object variables’ types. Declaring your object variables means that:

  • The bbjcpl compiler can perform type-checking (finding incorrect object type problems that might result in runtime errors).

  • Your code is more readable and maintainable because it becomes obvious what type of objects you intended to put into a variable.

  • The Eclipse BDT editor can offer code completion suggestions; only variables with a declared type support code completion in the Eclipse BDT editor (BDT cannot offer suggestions for methods or fields if it does not know the object type).

Throughout the sample code we use the declare verb, as demonstrated by this line from Sample 1:

declare Speaker Speaker!

This code declares a variable named Speaker!. The exclamation point suffix means that Speaker! is an “object variable”, and it can contain any type of object. In this case, it will only hold objects whose type is the class, Speaker.

With this general information, let’s write some more complicated code.

Conclusion

Custom objects bring the power of object-oriented programming to the BBj language, offering a modern paradigm for developing more maintainable code using Business BASIC. This introduction should contain enough information so that you can write and use custom objects in your BBj code.

More Information

Please post any additional questions or comments to bbj-developer@basis.cloud. To join that discussion forum, subscribe online. For information on using BASIS' community forums, including the bbj-developer list, please see the BASIS Community Forum User's Guide.

Next Step: Example Program #1

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