Link to home
Start Free TrialLog in
Avatar of IT79637
IT79637Flag for United States of America

asked on

How to call a function in another unit?

Hi Experts,

I have two programs.  
1. Unit1.pas is the main program.
2. search.pas contains functions that I want to call from the main program (Unit1.pas).

Below is a function from search.pas. The function call passes two strings to the function and returns an integer;

How would I code the call from the main program?

Thanks
Function from search.pas
-------------------------------
 
function PosInFile(Str, FileName: string): integer;
  function NextChar: char;
  begin
    if BufPtr >= BufEnd then
    begin
      BlockRead(F, Buffer, MAXFILESIZE, BufEnd);
       //ShowMessage('Char Read: '+IntToStr(BufEnd));
      BufPtr := 0;
      Application.ProcessMessages;
    end;
    Result := Buffer[BufPtr];
    Inc(BufPtr);
  end;
 
begin
  Result := -1;
  AssignFile(F, FileName);
  Reset(F, 1);
  BufPtr := 0;
  BufEnd := 0;
  Index := 0;
  Increment := 1;
  repeat
    c := NextChar;
    if c = Str[Increment] then
      Inc(Increment)
    else
    begin
      Inc(Index, Increment);
      Increment := 1;
    end;
    if Increment = (Length(Str) + 1) then
    begin
      Result := Index;
      break;
    end;
  until BufEnd = 0;
 
end;

Open in new window

SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT79637

ASKER

I renamed search.pas to Function.pas to make the name more general.

Below in the code section is procedure FindOCRString(); which is in the main program, Unti1.  Below that is the entire program Functions;

This is how I make the function call from Unit1 (main program):  posBegin := PosInFile(Str, FileName);

It throws compile error: Undeclared Identifier: "PosInFile".

Spelling seems to be correct.

I don't understand why?  

Thanks.



Uni1.pas
---------
procedure FindOCRString();
var
 Str : string;
 i, posBegin, posEnd : integer;
begin
  for i:=1 to NoDocsInBatch do begin
    Str:='#/';
    FileName := 'z:\TEMP\0000000051.tif.txt';
    posBegin := PosInFile(Str, FileName);
    Str:='/#';
    posEnd := PosInFile(Str, FileName);
    List := TStringList.Create;
    try
      List.Delimiter := '|';
      List.DelimitedText := OCRString;
      VendorNo := List[0];
      InvoiceNo := List[1];
      InvoiceDate := List[2];
      PO := List[3];
      SubTotal := List[4];
      Tax := List[5];
      Shipping := List[6];
      Deposit := List[7];
      BalanceDue := List[8];
      //ShowMessage(VendorNo+'|'+InvoiceNo+'|'+InvoiceDate+'|'+ PO+'|'+SubTotal+'|'+Tax+'|'+Shipping+'|'+Deposit+'|'+BalanceDue);
    finally
      List.Free;
    end;
  end;
end;
 
 
unit Functions;
 
interface
 
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, mySQLDirectQuery, mySQLDbTables;
 
const
  MAXFILESIZE = 16383;
 
var
  OCRString: string;
  posBegin, posEnd: Integer;
  Buffer: array[0..MAXFILESIZE] of char;
  FileName, Str, ImageName: string;
  BufPtr, BufEnd: integer;
  F: file;
  Index: integer;
  Increment: integer;
  c: char;
  VendorNo, InvoiceNo, InvoiceDate, PO, Tax, Shipping, SubTotal, Deposit, BalanceDue: string;
 
  List: TStringList;
 
implementation
 
function PosInFile(Str, FileName: string): integer;
  function NextChar: char;
  begin
    if BufPtr >= BufEnd then
    begin
      BlockRead(F, Buffer, MAXFILESIZE, BufEnd);
       //ShowMessage('Char Read: '+IntToStr(BufEnd));
      BufPtr := 0;
      Application.ProcessMessages;
    end;
    Result := Buffer[BufPtr];
    Inc(BufPtr);
  end;
 
begin
  Result := -1;
  AssignFile(F, FileName);
  Reset(F, 1);
  BufPtr := 0;
  BufEnd := 0;
  Index := 0;
  Increment := 1;
  repeat
    c := NextChar;
    if c = Str[Increment] then
      Inc(Increment)
    else
    begin
      Inc(Index, Increment);
      Increment := 1;
    end;
    if Increment = (Length(Str) + 1) then
    begin
      Result := Index;
      break;
    end;
  until BufEnd = 0;
 
end;
 
function RightStr
    (Const Str: String; Size: Word): String;
begin
  if Size > Length(Str) then Size := Length(Str) ;
  RightStr := Copy(Str, Length(Str)-Size+1, Size)
end;
 
function MidStr
    (Const Str: String; From, Size: Word): String;
begin
  MidStr := Copy(Str, From, Size)
end;
 
function LeftStr
    (Const Str: String; Size: Word): String;
begin
  LeftStr := Copy(Str, 1, Size)
end;
 
end.

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT79637

ASKER

OMY!!! So easy.  Sorry I asked.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT79637

ASKER

Up points to give credit due to all responders.  Gave few extra points to tankergoblin because he was very kind in supplying a code sample.  If anyone objects, please contact me.

Thanks to all.

dear bokist
i tried your way, but I got the following error
"This form of method call only allowed for class methods"

what should I do, I have a function "SendEmail" that will be used in all my units.pas and I want to write that function one time and call it from all the units
thank you
@Amanda77,
For openers, start your own question. ;-)
Do so and I will supply an answer. ;-)