Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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?
0
ginsonic
Asked:
ginsonic
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
geobulCommented:
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
 
david_barkerCommented:
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
 
ginsonicAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
BdLmCommented:
if you call functions from other units you have to add  them with the unit command. did you do this already?
0
 
geobulCommented:
Add the unit name where ShowEditor is declared in USES clause of the unit where TEditor is declared.
0
 
ginsonicAuthor Commented:
done already , but don't recognize if is declared inside TEditor.
0
 
BdLmCommented:
what ths exact compiler error message? can you post your unit teditor and the calling unit?
0
 
joncmoraCommented:
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
 
ginsonicAuthor Commented:
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
 
geobulCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now