Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Working with objects and functions in other units


How can I call a function or procedure in another unit, or retrieve and set the value of a variable in another unit?   

    Anything defined in the interface section of a unit is visible and callable from another unit that uses that unit. This includes Types, Constants, Classes, Functions, and Procedures. You should simply add the unit's name you want to use to the uses clause of the unit you wish to use it from. Unless necessary, you should define the uses clause in the implementation section, to help avoid circular reference errors. This is where two units depend on each other in their interface sections.

    If you have a function in the current unit that has the same name as a function in another unit your wish to call, you can preface the function name with the unit name to fully qualify it.

    The following example demonstrates calling functions and procedures and accessing variables in another unit:

    Example:

    {Code for unit2}

    unit Unit2;

    interface

    procedure SomeProcedure(i : integer); function SomeFunction(i : integer) : integer;

    var SomeInteger : integer;

    implementation

    procedure SomeProcedure(i : integer); begin SomeInteger := i; end;

    function SomeFunction(i : integer) : integer; begin Result := SomeInteger; SomeInteger := i; end;

    end. {Code for unit 's implementation section:}

    implementation uses Unit2;

    {$R *.DFM}

    procedure TForm .Button Click(Sender: TObject); begin SomeProcedure( 0); ShowMessage(IntToStr(SomeFunction( 2))); ShowMessage(IntToStr(SomeInteger)); end;

    procedure TForm .Button2Click(Sender: TObject); begin {Lets do the same thing as Button Click but fully qualify the names} Unit2.SomeProcedure( 0); ShowMessage(IntToStr(Unit2.SomeFunction( 2))); ShowMessage(IntToStr(Unit2.SomeInteger)); end;

    7/ 6/98 4:3 :28 PM

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use