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
LVL 2
SupWangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.