Solved

Declaring Global Variables

Posted on 2004-03-31
8
594 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

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