Getting a number from APW

Now we will write the function that uses the GetApwNumber function. This will be the Add function.

Create a new unit and save it as code.pas. Add this code after the interface keyword.

uses
    wintypes, sysutils, apw, apwpost;

function Add( wnd : hWnd ) : Integer ; export ; {$ifdef WIN32} stdcall ; {$endif}

The Add function doesn't take the values to add. Instead we will get the value of two APW variables called num1 and num2 and return the result of adding them together. Later on we will change the function so that instead of returning the result, it will change another APW variable. We will also change the function so that it will not need to have the window handle passed to it. But, one step at a time.

This is the completed Add function. Add it after the implementation keyword.

function Add( wnd : hWnd ) : Integer ;
var
   num1 , num2 : LongInt;
begin
   Result := 0;
   if GetApwNumber( 'num1' , num1 , wnd ) = true then
      if GetApwNumber( 'num2' , num2 , wnd ) = true then
         result := num1 + num2;
end;

The GetApwNumber function returns true or false depending on whether or not the value was successfully retrieved. You must check that the function succeeds before using the variable. In a real UCD, you should have some strategy in place to cope with this possibility. In this example, 0 is returned if an error occurs.

On to: Testing the Add function

Back to: The GetApwNumber function      Start