Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Separate Unit for Functions

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
i've got unit1:
Code:
uses Unit1;

type
  TForm1 = class(TForm)
      ...

var
  Form1: TForm1;
  F: TF;

       ....

procedure bla bla;
begin
  F.Function(1);
end;

and in unit2 i have:
Code:
type
  TF = class
  public
    function 1;
    function 2;
    function 3;
    function 4;
           ......
    function 172;
end;

implementation

function 1;
begin

end;

                 .......

function 172;
begin

end;

so, to call a function from unit2 in unit1 i have to F.Function, no problem there.

the main objective here is to have a unit full of functions, that i can call from other unit(s).

the thing is, unit2 will have a huge amout of functions, so i was wondering if there is any other way for this to work, without having to declare every function from unit2 in it's private section.

And, how do i refer to objects in unit1 from unit2's functions, if i can't declare unit1 in unit2's uses clause (causes error) ?

TIA !

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
why does unit 1 have a uses unit 1?

I do what you are trying to do all the time and have never had a problem with it.

I have a unit named FormatDate (all it used to have was date functions, it grew and I haven't fixed the "descriptive" name of the unit!):

Code:
unit FormatDate;

interface

uses StrUtils, SysUtils;

function ConvertDate(d: string): string;
function ConvertDateBack(d: string): string;
function IntToTime(iTime: integer): TDateTime;
function isDate ( const DateString: string ): boolean;
Function IsNumeric(Str:String):Boolean;
function ProperCase(strInput: string): string;

implementation

function ProperCase(strInput: string): string;
// change a string to Proper Case,
// ie ProperCase('metro court') returns 'Metro Court'.
var
  strTemp: string;
  J: integer;
begin
  Result:= EmptyStr;

  if strInput = EmptyStr
  then Exit;

  strTemp:= LowerCase(Trim(strInput));
  strTemp[1]:= UpCase(strTemp[1]);  // capitalize first letter.
  for J:= 2 to Length(strTemp) do
  begin
    // capitalize first letter after a space or ',' or '-' .
    if ((strTemp[J] = ' ') or (strTemp[J] = ',') or (strTemp[J] = '-'))
    then strTemp[J + 1]:= UpCase(strTemp[J + 1]);
  end;

  // This part changes II or III (the second, or, the third, part of a name)
  // into all caps (only works on first instance in a string):

  strTemp:= strTemp + ' ';
  J:= Pos(' ii ', LowerCase(strTemp));

  if J <> 0
  then
    begin
      strTemp[J + 1]:= 'I';
      strTemp[J + 2]:= 'I';
    end;

  J:= Pos(' iii ', LowerCase(strTemp));

  if J <> 0
  then
    begin
      strTemp[J + 1]:= 'I';
      strTemp[J + 2]:= 'I';
      strTemp[J + 3]:= 'I';
    end;

  Result:= Trim(strTemp);

end;


function ConvertDate(d: string): string;
begin
  Result := copy(d, 5, 2) + '/' + copy(d, 7, 2) + '/' + copy(d, 1, 4);
end;

function ConvertDateBack(d: string): string;
begin
  d := FormatDateTime('mm/dd/yyyy', strToDate(d));
  Result := copy(d, 7, 4) + copy(d, 1, 2) + copy(d, 4, 2);
end;

function IntToTime(iTime: integer): TDateTime;
// converts integer time (800) to TDateTime (8:00 am):
var
  strTime: string;
begin
  strTime:= IntToStr(iTime);
  while Length(strTime) < 4 do strTime:= '0' + strTime;
  strTime:= LeftStr(strTime, 2) + ':' + RightStr(strTime, 2);
  Result:= StrToTime(strTime);
end;

function isDate ( const DateString: string ): boolean;
begin
  try
    StrToDate ( DateString );
    result := true;
  except
    result := false;
  end;
end;

Function IsNumeric(Str:String):Boolean;
Var
   I:Integer;
Begin
   I:=1;
   While (I<=Length(Str)) do
   Begin
     If Not (Str[i] in ['0'..'9']) Then
     Begin
       Result:=False;
       Exit;
     End;
      Inc(I);
   End;
   Result:=True;
end;


end.

anytime I want to use any of these functions, I add FormatDate to the uses clause and it's available!

Leslie
 
jamesp0tter said:
And, how do i refer to objects in unit1 from unit2's functions, if i can't declare unit1 in unit2's uses clause (causes error)?

You don't refer back to the objects, you pass them in as parameters.

For example, I have a generic function for setting parameters when using dynamic SQL. It looks something like this:
Code:
function SetQueryParam(var aQuery: TQuery; aName: String; aDataType: TFieldType; aParamType: TParamType; aValue: Variant): Boolean;
begin
  try
    aQuery.ParamByName(aName).DataType := aDataType;
    aQuery.ParamByName(aName).ParamType := aParamType;
    aQuery.ParamByName(aName).Value := aValue;
    result := True;
  except
    result := False;
  end;
end;
My function does not have to refer back to the calling form because it has a reference to the TQuery sent in as a parameter.

-Dell
 
thank you both, i got my problem solved :)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top