The "this" keyword

The this keyword provides a predefined qualifier that refers to the container (the generatable logic part) that holds the current function. Consider the following situation: A program declares a global variable named runningTotal, then, from the main() function, calls the function getCustomer(). That function declares its own local variable named runningTotal. The original runningTotal is theoretically in scope within the function, but the unqualified name now refers to the local version. To access the original, use the qualifier this:
runningTotal = this.runningTotal + myCustomer.customerBalance;
Here the local runningTotal variable is initialized with the value of the runningTotal variable from main(), then the balance from the current customer is added to the local total.

In rare cases, you can use the this keyword to override a behavior of a set-value block in an assignment statement. Here this establishes the scope as being the declaration in which the set-value block resides. For details, see Set-value blocks.

Example

Assume you have a program, myProgramA, that calls a function, main(), that in turn calls myFunctionB(). Assume that each of these parts declares a variable, varX:
program myProgramA type BasicProgram
varX STRING = "program";

   function main()
      varX STRING = "main";
      myFunctionB();
   end

   function myFunctionB()
      varX STRING = "Function B";
      writeStdErr(this.varX);
   end
end

The variable this.varX displays the value "program" on the console, because the program is the generatable logic part that holds myFunctionB().


Feedback