• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How can I declare a full-range variable and procedure/function?

Hi all,
How can I declare a full-range variable and procedure/function?

e.g.
In form1,
I declare a variable and procedure/function like this:

  public
    { public declarations }
    TmpInt: Integar;
    procedure SetButton;
    ....
    ....
  end;


In form2, If I need to use "TmpInt" and "SetButton" then I must use Form1.TmpInt and Form1.SetButton to access them.  Can I just use "TmpInt and SetButton" instead of "the Form1.TmpInt and Form1.SetButton"? How can I declare a full-range variable and procedure/function? Decalare them
in DPR file? How to do that?

Thanks, supwang
0
SupWang
Asked:
SupWang
  • 4
  • 4
  • 2
1 Solution
 
TheNeilCommented:
Easy. Just declare the function/procedure/variable OUTSIDE the class. You just then need to make sure that any unit using them inherits the unit that they're defined in in the USES section (like you would normally)

e.g.
....
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure SetButton;

var
  Form1: TForm1;
  iGlobal_Int : INTEGER;

implementation
....


The variable iGlobal_Int and the procedure SetButton are now available globally without having to use 'Form1.'

The Neil
0
 
SupWangAuthor Commented:
wow, That's Ok. hehe. :-) Thanks.
BTW. What's the difference with the following code?

--------------------------------------->
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function Calculate(FirstValue, SecondValue: Integer): Integer;
begin
  Result := FirstValue*SecondValue;
end;

end.
---------------------------------------<

--------------------------------------->
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    function Calculate(FirstValue, SecondValue: Integer): Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function TForm1.Calculate(FirstValue, SecondValue: Integer): Integer;
begin
  Result := FirstValue*SecondValue;
end;

end.
---------------------------------------<


0
 
TheNeilCommented:
Well, in the first piece of code there's no declaration for the function at all (is this a trick question?).
Assuming that you DID have the correct declaration, the only difference would be in how you called the routine from another unit: In the first piece of code it would just be Calculate and in the second it would have to be Form1.Calculate

Do I get the points then?

The Neil
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ptmcompCommented:
Hello SupWang

Your first declared function is a simple function. The second is a method of an instance of an object, so depending of the values of some object fields a method could do diffrent things. e.g.:

type
  TForm1 = class(TForm)
    procedure ButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FirstValue, SecondValue: Integer; // are now fields of the object
    function Calculate: Integer;  // don't need parameters any more cause there are stored in the object
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function TForm1.Calculate: Integer;
begin
  Result := FirstValue*SecondValue;
end;

procedure TForm1.ButtonClick(Sender: TObject);
begin
  FirstValue:=2;
  SecondValue:=3;
  // Now the fields are set and you can do your calculation:
  ShowMessage(IntToStr(Calculate));
end;

Regards, ptm.
0
 
SupWangAuthor Commented:
Hi Neil,
Can't call the routine form another unit. In the second I put the declare to private section, so can't. Also the first way can't.

Hi ptm,
Why change declare from private section to public section?

Regards, supwang
0
 
ptmcompCommented:
Oh, it's just an exmaple but I didn't see any reason to hide this function for other units, so it put it in the public section.
e.g:
unit Unit3;

interface

uses
  Windows{...},
  unit1;

type
  TForm3 = class(TForm)
    procedure Button1Click(Sender: TObject);  
  private
    aForm1: TForm1;
  public
  end;

implementation

procedure TForm3.Button1Click(Sender: TObject);
begin
  aForm1:=TForm1.Create(Self);
  try
    aForm1.FirstValue:=7;
    aForm1.SecondValue:=9;
    ShowMessage(IntToStr(aForm1.Calculate));
  finally
   aForm1.Free;
  end;
end;

OK, it doesn't make a lot sense to use a form this way. I want to show why it can make sense to declare the method as public method.

0
 
SupWangAuthor Commented:
Hi ptm,
You declare a variable in the private section, but in my example, I declare a function in the private section. More ideas?

Regards, SupWang
0
 
ptmcompCommented:
The big diffrence is that my "Calculate" is a method an yours was a procedure. I wanted to show you the diffrence and possiblities of them. Or what's exact your question?
0
 
ptmcompCommented:
The big diffrence is that my "Calculate" is a method an yours was a procedure. I wanted to show you the diffrence and possiblities of them. Or what's exact your question?
0
 
SupWangAuthor Commented:
Hi  ptmcomp,
You are right. Please go to here and get the points.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10263793 


Thanks, supwang

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now