• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2453
  • Last Modified:

Passing Variables Between Forms Without Using Global Variables

I have a Delphi application built in Delphi 2006. It is a VCL desktop app. I want to reduce my use of global variables. I need to receive and pass data among forms but I have not found an easy way to do that since to reduce memory usage I close the form when the user finishes entering the new data. I am using global variables to capture the information now. I want to find another way? Does anyone have any suggestions?
0
mcmahling
Asked:
mcmahling
3 Solutions
 
rfwoolfCommented:
You could store information into an INI file or a database.
For INI Files...
add INIFiles to your uses clause for a unit you want to use inifiles...
To write to an INI file:
var
    myINI : TINIFile;
begin
    myINI := TINIFile.Create(ExtractFilePath(Application.EXEName) + 'myinifile.ini');
    myINI.WriteString('Settings', 'Text Box', txtValue.Text);
    myINI.Free;
end;

To read from an INI file:
var
    myINI : TINIFile;
begin
    myINI := TINIFile.Create(ExtractFilePath(Application.EXEName) + 'myinifile.ini');
    txtValue.Text := myINI.ReadString('Settings', 'Text Box', 'Default');
    myINI.Free;
end;  
0
 
BdLmCommented:
from the oop software aspect the properies methose would be the best
0
 
BdLmCommented:
//  simple code fragment

MyClass = Class(.....)

    //  private

   FNumber :  Integer ;   //  this value is only visible inside the class

    //  public

 
  procedure SetNumber (AVal : Integer) ;  //  if you set this value,
                                                                         //  do some checking eg. range ..... here


   property  aNumber :  Integer  read FNumber  write SetNumber;





calling code:
 
    MyClass.aNumber := 5;    // if do this the setNumberfunctions will be executed
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
BdLmCommented:
other option:

define a new record or a class:

     TuserInput = record
         firstdata :  string;
         id :  Integer;
         ....
        ....
        end;


definition


var   MyData : TuserInput;




properties and user defined data types are of course also possible



0
 
mcmahlingAuthor Commented:
BdLm:

Thank you for your reply. To see MyClass from all the forms would I have to declare in a certain way?
0
 
BdLmCommented:

to acess the data you need to include the units  ->   uses ....., ... ,  unit_myform;
the better way is to have one class for the GUI  (Form) and  one another class for the
data you exchange , have a look at google or wiki three layer software architecture  


(makes your code more easy to read and maintain)
0
 
Geert GOracle dbaCommented:
you use an instance of a stringlist to pass information
with the values option

like passing a name
List.Values['FIRST_NAME'] := 'Ed';

to retreive
  ShowMessage(List.Values['FIRST_NAME']);
or
  Edit1.Text := List.Values['FIRST_NAME'];

and returning it
  List.Values['FIRST_NAME'] := Edit1.Text;

there should be a sample somewhere on this site ...
0
 
Geert GOracle dbaCommented:
ah here is something like that ...
unit SubFormUnit;
 
interface 
 
uses Windows, SysUtils, Forms, ...;
 
type
  TSubForm = class(TForm)
  end;
 
var SubForm: TSubForm;
 
function OpenSubForm(AOwner: TComponent; var Data: TStrings): boolean;
 
implementation
  
function OpenSubForm(AOwner: TComponent; var Data: TStrings): boolean;
begin
  Result := False;
  SubForm := TSubForm.Create(AOwner);
  try
    SubForm.EditSub1.Text := Data.Values['EDIT1'];
    ...
    if SubForm.ShowModal = mrOk then 
    begin
       Result := True;
       Data.Values['EDIT1'] := SubForm.EditSub1.Text;
       ...
    end;
  finally
    FreeAndNil(SubForm);
  end;
end;
 
-----------
 
and in the mainform :
 
procedure TMainForm.EditData;
var Data: TStringList;
begin
  Data := TStringList.Create;
  try
    Data.Values['EDIT1'] := Edit1.Text;
    ...
    if OpenSubForm(Self, Data) then 
    begin
      Edit1.Text := Data.Values['EDIT1'];
      ...
    end;
  finally
    FreeAndNil(Data);
  end;
end;

Open in new window

0
 
Eddie ShipmanAll-around developerCommented:
Use a TDataModule and create a property on the Datamodule, use the Datamodule in all your forms and they will all have access to that property. When you want to modify it, you can just like any other variable.
0
 
Geert GOracle dbaCommented:
a B shouldn't go without a comment ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now