Solved

Pass from function use in all

Posted on 2004-10-07
10
147 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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