Solved

Declaring Global Variables

Posted on 2004-03-31
8
591 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:mnasman
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

14 Experts available now in Live!

Get 1:1 Help Now