Getting Started

From GAB Help
Jump to: navigation, search

Programming can be a little intimidating if you've never done it before, but like almost anything else it isn't an all-or-nothing venture. Programs that perform useful functions in Global Shop can be as short as a few lines. Generally speaking, if you can write out a process step-by-step in plain English or Spanish (also called pseudo-code), writing the program isn't difficult.


Time for something simple:

variable.local.result.declare(String)

function.intrinsic.UI.inputbox("What is your name?","Getting info",0,variable.local.result)

function.intrinsic.UI.msgbox(variable.local.result) 


Variable.local.result would return whatever the user entered into the input box. Then the message box would display that variable.

Variables are used in programming languages to temporarily hold values. The first line declares (creates) a LOCAL variable named "RESULT". The portion that reads "STRING" indicates the type of data the variable will be asked to hold. Valid types are as follows:

Boolean - This variable type holds either a True or False

Date - This variable type holds date or time information

Float - This variable type holds numeric information (decimal)

Long - This variable type holds numeric information (whole numbers)

String - This variable type holds text information


Using the proper variable types is important, because the declaration lets GTL know what operations are legal when handling the variable. For example, date commands work with DATE type variables, while MATH commands work with FLOAT and LONG variables.

Please note that all PASSED variables are declared as STRING.

The INPUTBOX function prompts the user for typed input; when the input is received, it stores this into the LOCAL.RESULT variable.

The MSGBOX function displays text in a dialog box. In this example, the passed text is the contents of the LOCAL.RESULT variable.

The Next Step...

The sample program in the last section accepted input, stored it to a variable, then displayed the contents of the variable. We can take it a step further and accept data from the calling program instead.

Variable.local.rdate.declare(date)

Variable.local.duedate.declare(string)

function.intrinsic.date.convertdstring(Variable.passed.000023,Variable.passed.000023.type,variable.local.rdate)

function.intrinsic.date.dateadd(“d”,2,variable.local.rdate,variable.local.rdate)

function.intrinsic.string.datestring(variable.local.rdate,variable.local.duedate)

variable.passed.000024.set(variable.local.duedate) 

This example takes the order date from the sales order entry screen, adds two days to the value, then places the new date into the due date field.

Conditional Processing

The last example allowed us to react to data passed from a Global Shop program, and populate a screen value with a calculated result. Automatic due date filling based on rules is something POS customers are accustomed to doing. The above code might not be desirable if there is already a due date present. Below is a pseudo-code example:

If something is true

then do this.....

and this...

and this

If something else is true 

then do this.....

and this...

and this

If none of the previous conditions were true

then do this.....

and this...

and this 


GTL handles this as an IF/ELSEIF/ELSE/ENDIF construction. Here's an example building on our existing code:

Variable.local.rdate.declare(date)

Variable.local.duedate.declare(string)

function.intrinsic.control.if(Variable.passed.000024,”=”,”000000”)

function.intrinsic.date.convertdstring(Variable.passed.000023,Variable.passed.000023.type,variable.local.rdate)

function.intrinsic.date.dateadd(“d”,2,variable.local.rdate,variable.local.rdate)

function.intrinsic.string.datestring(variable.local.rdate,variable.local.duedate)

variable.passed.000024.set(variable.local.duedate)

function.intrinsic.control.endif 

The “=” in the example above is called the operator. Valid operators are:

"=", "IS", "<>", "!=", "=!", "NOT", ">", "<", ">=", "=>", "<=", "=<"

The GAB documentation explains the use of these operators. Please note that several operators are functionally identical.

More Conditional Processing: From Code That Works to Code That Doesn't Break

With two lines of code, we were able to restrict the due date manipulation to orders that had no due date. Conditional structures are used extensively in good code to handle instances where the proper data is not passed. For example, if the order date (for whatever reason) is blank, the code above will blow up. Adding another condition to check for this possibility will make the code more robust.

Variable.local.rdate.declare(date)

Variable.local.duedate.declare(string)

function.intrinsic.control.if(Variable.passed.000023,”<>”,”000000”)

function.intrinsic.control.if(Variable.passed.000024,”=”,”000000”)

function.intrinsic.date.convertdstring(Variable.passed.000023,Variable.passed.000023.type,variable.local.rdate)

function.intrinsic.date.dateadd(“d”,2,variable.local.rdate,variable.local.rdate)

function.intrinsic.string.datestring(variable.local.rdate,variable.local.duedate)

variable.passed.000024.set(variable.local.duedate)

function.intrinsic.control.endif

function.intrinsic.control.endif 

This example features nested IF statements. Put another way, the IF statements are “layered”, so the second IF statement is evaluated only if the first IF statement was found to be true.

Looping

While control structures are the most important part of a programming language, the ability to loop is a close second. If you need to evaluate more than a single piece of data (perhaps for data aggregation), you will need to have the ability to evaluate each piece of information. Looping is the means by which you tell the computer to look at each item in order.

If you know how many times you want to loop (ex. number of months in a year), you can use a FOR loop. A FOR loop requires a control variable (usually a LOCAL variable declared as a LONG). Below is a pseudo-code example:

for i = 1 to number of days in the month

add up the labor hours for day i

store the result of the sum in a bucket for day i

next i

If you aren't sure how many times you need to loop (ex. traversing a set of database records), you can use a DO loop. Below is a pseudo-code example:

do

act on the next piece of data

if some condition is met, exit the loop

loop

Loops, like conditional statements, can be nested for multiple levels of looping. A common example of this may be processing all of the days in a month, for each month of a year, for all of your workcenters individually – that would be three nested loops.

Making the Example More Practical Through Looping

The example we've been building seems pretty straightforward, and looping may not appear to be necessary on the surface. Digging a bit deeper into the usage, however, reveals weaknesses. What if the order is taken on a Thursday? In that case, the order will be due on a Saturday. Let's fix it:

Variable.local.rdate.declare(date)

Variable.local.duedate.declare(string)

Variable.local.daysremain.declare(long)

Variable.local.daytype.declare(long)

Variable.local.daysremain.set(2)

function.intrinsic.control.if(Variable.passed.000023,”<>”,”000000”)

function.intrinsic.control.if(Variable.passed.000024,”=”,”000000”)

function.intrinsic.date.convertdstring(Variable.passed.000023,Variable.passed.000023.type,variable.local.rdate)

function.intrinsic.control.dountil(Variable.local.daysremain,”=”,0)

function.intrinsic.date.dateadd(“d,1,variable.local.rdate,variable.local.rdate)

function.intrinsic.date.weekday(variable.local.rdate,Variable.local.daytype)

function.intrinsic.control.if(Variable.local.daytype,”>”,1)

function.intrinsic.control.andif(Variable.local.daytype,”<”,7) 

function.intrinsic.math.sub(Variable.local.daysremain,1,Variable.local.daysremain)

function.intrinsic.control.endif

function.intrinsic.date.dateadd(“d,1,variable.local.rdate,variable.local.rdate)

function.intrinsic.control.loop

function.intrinsic.string.datestring(variable.local.rdate,variable.local.duedate)

variable.passed.000024.set(variable.local.duedate)

function.intrinsic.control.endif

function.intrinsic.control.endif  

GAB Line Layout

Syntax

Identifier.subidentifier.nominative.property/method.name arguments

Definitions

Identifier - Function, GUI, Program, or Variable

SubIdentifier -

Nominative - Name variable or name of variable with array, screen and screen element. Delimited by !

Property/Method -

Arguments -

Example

Function.Intrinsic.txtA.declare (args)

Style

Comments - Adding comments to your code allows anyone to go into your code and be able to figure out what the code is supposed to do in each section. To add a comment simple put a ‘ in front of the comment, the line should turn green in the Editor. No in-line comments are allowed.

Comments can be used for:

  • Showing a modification user log
  • Showing a Change Log
  • Explaining what the code is supposed to do

Indention – Using indentation with control structures (“if” blocks and loops) helps with readability. Use continued indention for each subsequent control block.

‘Outer If

Function.Intrinsic.Control.if(Variable.Passed.000052,<>,"")

‘Inner If

Function.Intrinsic.Control.If(Variable.Passed.000012,<>,"00000000.0000")

    ‘Stuff to do inside the if statement

    Function.Intrinsic.Math.Add(Variable.Passed.000012,1,Variable.Local.iCost)

    Variable.Passed.000022.Set(Variable.Local.iCost)

    Function.Intrinsic.Control.ElseIf(Variable.Passed.000012,=,"00000000.0000")

    Function.Intrinsic.ui.Msgbox("There is no cost","",0)

    ‘Ends the program/project

    Function.Intrinsic.Control.End

‘Ends the Inner If

Function.Intrinsic.Control.EndIf

‘Ends the Outer If

Function.Intrinsic.Control.EndIf  

Shortcuts

V.AMBIENT. = Variable.Ambient.

V.ARGS. = Variable.Args.

V.CALLER. = Variable.Caller.

V.COLOR. = Variable.Color.

V.EXTERNAL. = Variable.External.

V.GLOBAL. = Variable.Global.

V.LOCAL. = Variable.Local.

V.ODBC. = Variable.ODBC.

V.PASSED. = Variable.Passed.

V.SCREEN. = Variable.Screen.

V.STATIC. = Variable.Static.

F.AUTOMATION. = Function.Automation.

F.COMMUNICATION. = Function.Communication.

F.INTRINSIC. = Function.Intrinsic.

F.GLOBAL. = Function.Global.

F.ODBC. = Function.ODBC.

F.UNOFFICIAL. = Function.Unofficial.

P.SUB. = Program.Sub