Solved

Pass from function use in all

Posted on 2004-10-07
10
145 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
indy Tidtcpserver best  way to brodcast message to multiable clients 1 145
Convert Jpg /PNG To GIF 5 120
Simple Delphi Question 9 83
delphi parse string to params 3 100
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

12 Experts available now in Live!

Get 1:1 Help Now