Solved

Pass from function use in all

Posted on 2004-10-07
10
140 Views
Last Modified: 2010-04-05
I receive an object like:

function DoSomething (ABar: TgnMenuBar): Boolean;
begin
  blablabla
end;

I wish to use ABar into a different procedure:

procedure ButtonClick...
  ABar.Color:= clRed;
end;

How to do this? How to declar the ABar as to can use it outside my function?
0
Comment
Question by:ginsonic
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 12249761
Hi,

To use DoSomething you have to write code like:

var ABar: TgnMenuBar;
begin
  if DoSomething (ABar) then begin
    ...
end;

Put the var declaration in interface section of the unit and you will be able to use ABar variable in your forms code.

unit ...
interface
...
var ABar: TgnMenuBar;

implementation

procedure GetABar;
begin
  if DoSomething (ABar) then begin
    ...
end;

// in the same or another unit:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ABar.Color:= clRed;
end;

Regards, Geo
0
 
LVL 6

Assisted Solution

by:david_barker
david_barker earned 200 total points
ID: 12249772
Declare a variable of like
myMenyBar : TgnMenuBar;
in the private section of your form.

Make sure that DoSomething and ButtonClick are also declared in your form and defined as

function TForm1.DoSomething (ABar: TgnMenuBar): Boolean;
and
procedure TForm1.ButtonClick...

then do something like :

function DoSomething (ABar: TgnMenuBar): Boolean;
begin
  myMenyBar:=ABar;  // assign to my variable
  blablabla
end;

procedure ButtonClick...
  myMenuBar.Color:= clRed;  // changed to my variable
end;


0
 
LVL 9

Author Comment

by:ginsonic
ID: 12250326
Have a problem. I call my function with

0: if ShowEditor(TgnMenuBar(Component)) then
         Designer.Modified;

If add ShowEditor to TEditor , how you say, my compiler tell me that is unknown ( call from a different unit ).

On this moment my function is declared outside the TEditor class.
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12250617
if you call functions from other units you have to add  them with the unit command. did you do this already?
0
 
LVL 17

Expert Comment

by:geobul
ID: 12250859
Add the unit name where ShowEditor is declared in USES clause of the unit where TEditor is declared.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:ginsonic
ID: 12250998
done already , but don't recognize if is declared inside TEditor.
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12251339
what ths exact compiler error message? can you post your unit teditor and the calling unit?
0
 

Expert Comment

by:joncmora
ID: 12255601
Put the variable declaration of your TgnMenuBar on the interface portion. Put it before the type declaration of TEditor. Have it look something like this

-------------------------
unit MyUnit;

interface

uses
  {Other units}, UnitContainingDeclarationOf_TgnMenuBar;

var
  MyBar: TgnMenuBar;

type
  TEditor = class(TCustomEditor)
    {Field declarations}
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


-------------------------
In your other units, use the unit in the interface section too.


unit MyOtherUnit;

interface

uses
  {Other units}, MyUnit, UnitContainingDeclarationOf_TgnMenuBar;
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12256057
Done already :) But ... [Error] gnMenuBar.pas(1247): Undeclared identifier: 'ShowEditor'

Unit1 IS the TgnMenuBar declaration ( is my vcl :) ) and try to call my editor from Unit2.
0
 
LVL 17

Accepted Solution

by:
geobul earned 300 total points
ID: 12256652
Hi,

Perhaps your ShowEditor function is not declared in the interface section of its unit:
-------
unit UnitContainingShowEditor;

interface

function ShowEditor(ABar: TgnMenuBar): Boolean;

implementation

function ShowEditor(ABar: TgnMenuBar): Boolean;
begin
  blablabla
end;

end.
----------
and then the unit where TgnMenuBar uses it:
-------
unit gnMenuBar;

interface

type TgnMenuBar ....

implementation

uses UnitContainingShowEditor;

....
procedure TgnMenuBar.AMethod;
begin
  ...
  if ShowEditor(TgnMenuBar(Component)) then
         Designer.Modified;
  ...
end;

end.
---------
Regards, Geo
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now