0
votes

I want to store text from ini file into a variable and use the var many times along my setup pages,

For instace lets say I have ini file named-"datatxt", ini section -[txtdata], section key-"htxt", key value - "hello world". I want to store this text value in a var named - "hiVar" and use it here-

Title := 
    TNewStaticText.Create(MOPage);
    Title.Parent := MOPage.Surface;
    Title.Font.Name := 'Verdana';
    Title.Caption := hiVar; 
1

1 Answers

1
votes

For declaring variables there are two scopes available. Local and global. Locally declared variables are visible only within the body of a procedure or method where they are declared. They are widely used as a temporary storage for intermediate operations, or for holding object references (as you already do):

procedure DoSomething;
var
  S: string; // <- this is a locally declared variable
begin
  // only inside this procedure the S variable can be accessed
end;

Globally declared variables (which is your question about) are visible in the scope of all procedures and methods within the whole code scripting section. They are used for holding references of objects whose are used across script code, passing results of some operations between event methods, or for holding some permanent values (which is your case):

var
  S: string; // <- this is a globally declared variable

procedure DoSomething;
begin
  // inside this procedure the S variable can be accessed
end;

procedure DoSomethingElse;
begin
  // as well as inside this procedure the S variable can be accessed
end;

Answer your question with an example is quite hard, since you haven't desribed the context in which you want to read that INI file, so it's hard to tell in which event you should read it. In the following example the INI file value is read when the wizard form is initialized. You can see there an access to a global variable from another method as well:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Files]
Source: "datatxt"; Flags: dontcopy

[Code]
var
  hiVar: string; // <- this is a globally declared variable

procedure InitializeWizard;
begin
  // extract the file into the setup temporary folder
  ExtractTemporaryFile('datatxt');
  // assign the read value into a global variable
  hiVar := GetIniString('txtdata', 'htxt', '', ExpandConstant('{tmp}\datatxt'));
  // from now on the variable should contain the key value
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // just a random code showing that you can access global
  // variables across the methods
  if CurPageID = wpWelcome then
    MsgBox(hiVar, mbInformation, MB_OK);
end;