Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

Pass from function use in all

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?
Avatar of geobul
geobul

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
SOLUTION
Avatar of david_barker
david_barker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ginsonic

ASKER

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.
if you call functions from other units you have to add  them with the unit command. did you do this already?
Add the unit name where ShowEditor is declared in USES clause of the unit where TEditor is declared.
done already , but don't recognize if is declared inside TEditor.
what ths exact compiler error message? can you post your unit teditor and the calling unit?
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;
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial