Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing Variables Between Forms Without Using Global Variables

Posted on 2009-04-20
10
Medium Priority
?
2,360 Views
Last Modified: 2012-08-13
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
Comment
Question by:mcmahling
10 Comments
 
LVL 13

Assisted Solution

by:rfwoolf
rfwoolf earned 300 total points
ID: 24184148
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
 
LVL 8

Expert Comment

by:BdLm
ID: 24185364
from the oop software aspect the properies methose would be the best
0
 
LVL 8

Accepted Solution

by:
BdLm earned 600 total points
ID: 24185465
//  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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:BdLm
ID: 24185529
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
 

Author Comment

by:mcmahling
ID: 24185532
BdLm:

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

Expert Comment

by:BdLm
ID: 24185860

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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24191020
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
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 600 total points
ID: 24191029
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
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 24196048
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24241332
a B shouldn't go without a comment ...
0

Featured Post

Industry Leaders: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

581 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