Declaring Global Variables

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?
zicheAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
necro_ccsConnect With a Mentor Commented:
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
 
oraelbisCommented:
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
 
kretzschmarCommented:
>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
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.

 
Mohammed NasmanSoftware DeveloperCommented:
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
 
StTwisterCommented:
declare the variable in the public section in Unit1. To acces it from other forms just use Form1.String1.
0
 
d1kGroupCommented:
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
 
necro_ccsCommented:
correction I put in Unit1 uses unit2
not uses unit1... A little too quick on the submit button;)
0
 
kretzschmarCommented:
just wondering,

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

meikl ;-)
0
All Courses

From novice to tech pro — start learning today.