Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EDT:Tutorial: Access a database with EGL Rich UI Lesson 7

Revision as of 15:44, 24 October 2011 by Tdramsey.us.ibm.com (Talk | contribs) (New page: = Lesson 7: Create a library of reusable functions<br> = Create a library to format money values and to associate category numbers with descriptions.<br>Libraries contain functions, const...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Lesson 7: Create a library of reusable functions

Create a library to format money values and to associate category numbers with descriptions.
Libraries contain functions, constants, and variables that you can use in multiple locations.

When you reference a declaration in the library from other logic such as a service or handler, you can include the library name as a prefix. For example, MyLibrary.myLibraryVariable is appropriate if the library name is MyLibrary and the library includes the myLibraryVariable variable. Alternatively, you can include the library name in a use statement in the other logic and avoid the need to qualify every reference. In that case, myLibraryVariable is sufficient to reference that variable.

Create a Library part

To create a Library part:

Right-click the PaymentClient folder, then click New > Library.
In the New EGL Library window, enter the following information:
In the EGL source file name field, enter the following name:

PaymentLib

In the Package field, enter the following name:

libraries

Under EGL Library Type, leave the default value of Basic selected.
The new Library part opens in the EGL editor.
Replace the boilerplate code in the Library part with the following lines:

package libraries;

library PaymentLib type BasicLibrary {}

end

Save the file.


Create the categories array

Add the following code before the final end statement:

categories STRING[] = [

"Rent", // 1
"Food", // 2
"Entertainment", // 3
"Automotive", // 4
"Utilities", // 5
"Clothes", // 6
"Other" // 7
];

The value is an array, and as is true of all arrays in EGL, the index of the first element is 1, not 0.
The array is used in logic that acts as follows:

Places an expense category into the database in integer form, to save space.
Places the expense category onto the web page in string form, for clarity.


Create the get functions for categories


The next functions convert between the following two formats for expense categories: integer and string.

Add the following code before the final end statement:

function getCategoryDesc(cat INT in) returns(STRING)
if(cat) // the integer is not 0
return(categories[cat]);
else
return("");
end
end

The function receives the integer format of an expense category and returns the related array element. If the input value is 0, the function returns an empty string.
Add the following code before the final end statement:

function getCategoryNum(desc STRING in) returns(INT)
for(i INT from 1 to categories.getSize())
if(categories[i] == desc)
return(i);
end
end
return(0); // no match
end

This function receives the string format of an expense category and returns the integer format, if possible. If no match is found for the received string, the function returns 0.
Format the file.
Save and close the PaymentLib Library. If you see errors in your source file, compare your code to the file contents in Finished code for PaymentLib.egl after lesson 7.

Related reference

Arrays

Lesson checkpoint

You learned how to complete the following tasks:

  • Create a Library part.
  • Add functions and a variable to a library.



Copyright © Eclipse Foundation, Inc. All Rights Reserved.