Solved

Declaring Global Variables

Posted on 2004-03-31
8
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

617 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