3.3 Package
What is Package?
Packages can contain classes, functions, and objects.
Package example (MyPackage.crs)
package MyPackage {
/* クラス */
class MyButton extends Button {
Title = "MyButton";
Function OnTouch(e) {
MessageBox("Hello");
}
}
/* ファンクション */
Function MyFunction(text) {
return MessageBox(text);
}
/* オブジェクト */
String MyData = "sample";
}
Today’s CRS has a limited concept of packages, and unlike other languages such as Java , package names do not affect class identification. For example, many languages allow you to qualify a class name with a package name, but CRS does not allow you to specify such a qualification. Therefore, the class name must be uniquely defined in every CRS loaded into Biz / Browser.
When using MyButton in the above example , write as follows.
MyButton btn {
X = 10;
Y = 10;
Width = 100;
Height = 30;
}
Functions included in a package can be called from any scope by qualifying them with the package name. When using MyFunction in the above example, write as follows.
MyPackage.MyFunction ("sample");
Objects included in a package can be similarly qualified with the package name and referenced from any scope. When using MyData in the above example , write as follows.
MessageBox (MyPackage.MyData);
Functions and data in the package can be used in the same way as importing to the global namespace using Record of ver3.0.
Using the Package?
Normally, the package is created as a CRS file separate from the application screen that defines Form etc. By defining application-specific classes, functions, constants, etc. in the package and importing them, you can use them like a library from other CRS programs. The following is an example of a screen that uses MyPackage .
Usage example (Form1.crs)
import "MyPackage.crs";
Form Form1 {
Width = 800;
Height = 600;
MyButton btn {
X = 10;
Y = 10;
Width = 120;
Height = 30;
}
}
In this way, you can develop efficiently by importing the CRS file that defines the package . Note that when loading a package with import , the filename must always be the package name with the extension “.crs” .
Points to keep in mind when using the package
Package name and file name
The file name of the package to be imported must have “.crs” added to the package name. Please note that if the file name and package name do not match, import will result in an error.
The behavior of the import statement is that if a package that matches the filename of the specified URL is already loaded in memory, it will return immediately without reloading the package. Unlike the GET method, this behavior does not involve caching, communication, compilation, and script execution behavior, so the second and subsequent imports complete very quickly.
Due to this characteristic, even if the same package is imported on multiple screens, only the first time is always executed, so it is possible to implement common processing with almost no overhead even when switching screens.
Package validity period
Once loaded into Biz / Browser , the package stays in memory until the session is reset by the //.login () and //.logout () methods. Note that defining too much data or too many functions in a package consumes a lot of memory.
Consideration for operating speed
Note that defining too much data or too many functions in a package can slow it down. If you need to define a lot of data, it is better to subdivide the package or to define it in a layered manner using Record object etc. in the package, which will have less effect on the operation speed.
package MyPackage {
Record dataRec1 {
String Data1 = "sample1";
String Data2 = "sample2";
:
}
Record dataRec2 {
String Data3 = "sample3";
String Data4 = "sample4";
:
}
:
It is not necessary to import all the packages provided by the application at the beginning of the application. You can import at any time, so it’s more efficient to import the packages you really need for each CRS file.
It is easier to develop a program by importing all the packages at once, but please be careful since packages that are not actually used are also loaded, it takes a long time to start and the application speed slows down.
*Mobile does not support packages