Solved

Declaring Global Variables

Posted on 2004-03-31
8
592 Views
Last Modified: 2010-08-05
How do you declare global variables, in a form or module, that can be accessed by other forms in the application?

The following code doesn't seem to work...

type
  TDModule1 = class(TDataModule)
    .........;
  private
    { Private declarations }
    STRING1: string;
  public
    { Public declarations }
  published
    { Published declarations }
    property STRINGOUT: String read STRING1 write STRING1;
  end;

Attempting to access this module from another form produses errors and the code refuses to compile (yes the module has been included in the form's uses area).

What am I doing wrong?
0
Comment
Question by:ziche
8 Comments
 
LVL 3

Expert Comment

by:oraelbis
ID: 10722199
Chek module creation order in  Project->Options->Forms
May be you try access to your datamodule from form, which created before your Data module creation.
It means that DataModule instance not created yet.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10722367
>How do you declare global variables, in a form or module,
>that can be accessed by other forms in the application?


type
  TDModule1 = class(TDataModule)
    .........;
  private
    { Private declarations }
  public
    { Public declarations }
  published
    { Published declarations }
  end;

var String1 : String = '';

implementation

....

meikl ;-)
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 10722556
Just place the variable on the public section of the class, if you would access it directly
then access it like this
DModule1.STRING1 := 'my value';
also read oraelbis comments, if your DataMoudle isn't before the unit you are trying to access the variable from, you will got access violation error, because it's not created yet, so make sure it's on the first of created forms, from project > options menu

meikl comments work too, you can access the variable directly without refering to the form or the datamodule
0
 
LVL 2

Expert Comment

by:StTwister
ID: 10726588
declare the variable in the public section in Unit1. To acces it from other forms just use Form1.String1.
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.

 

Expert Comment

by:d1kGroup
ID: 10726653
in unit interface on var section
all variable declared here are global (can be accesed from any unit which use your unit)
the rule is applied to all units even if unit has a form!
0
 

Accepted Solution

by:
necro_ccs earned 50 total points
ID: 10727426
Your code above should compile fine.. I did the following in D5
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TDataModule2 = class(TDataModule)
  private
    { Private declarations }
    FSomething : string;
  public
    { Public declarations }
  property
    Something : string  read FSomething write FSomething;
  end;

var

DataModule2: TDataModule2;
{.........etc}

Now in Unit1 (Form1) I put uses Unit1 and just tried
procedure TForm1.Button1Click(Sender: TObject);
begin
  DataModule2.Something := 'Testing write and read'; //test write
  ShowMessage (DataModule2.Something);//test read
end;

And this worked fine.
0
 

Expert Comment

by:necro_ccs
ID: 10727437
correction I put in Unit1 uses unit2
not uses unit1... A little too quick on the submit button;)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10730862
just wondering,

because a property is not a global var,
it may global for the object-instance

meikl ;-)
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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
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 …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

13 Experts available now in Live!

Get 1:1 Help Now