Solved

Any Simplification possible?

Posted on 2003-11-16
7
227 Views
Last Modified: 2010-04-05
Is there any way to simplyfy the following and make it a bit easier/shorter ?

procedure tLang.Load;
var lngIni : TMemIniFile;
begin
lngIni := TMemIniFile.Create('.\lang\'+Lang+'.lang');
 with lngIni do
  begin
  mFile := ReadString('frmMain','mFile','');
  mFilePrefs := ReadString('frmMain','mFilePrefs','');
  mFileQuit := ReadString('frmMain','mFileQuit','');
  mHelp := ReadString('frmMain','mHelp','');
  mHelpIndex := ReadString('frmMain','mHelpIndex','');
  mHelpUpdate := ReadString('frmMain','mHelpUpdate','');
  mHelpAbout := ReadString('frmMain','mHelpUpdate','');
  Free;
  end;
end;

In this case I'm reading a language .ini file for multi language support in my application.

Thx
0
Comment
Question by:Professor
7 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9759853
Me again! ;-)

Well, the first thing you can do is store 'frmMain' in a constant, which is good programming practise anyway.

The other thing you could do is store the field names in a TStringList with a pointer to the variable you wish to store it in. However, setting this up is about the same as doing it the way you have done it.

Geoff M.
0
 

Author Comment

by:Professor
ID: 9759868
How exactly would I have to do that constant thing ??

Sorry ... im kinda totally new to delphi ... Im more used to coding ancient pascal for Dos *lol*
0
 

Expert Comment

by:Smortex
ID: 9759875
Supposing mFile, mFilePrefs, ... are declared as string, you can do this :

MySoftStrings: array[0..6] of string;
[...]
with TIniFile.Create('.\lang\'+Lang+'.lang') do
try
  for i := Low(MySoftStrings) to High(MySoftStrings) do
    MySoftStrings[i] := ReadString('frmMain',IntToStr(i),'');
finally
  Free;
end;

The problem is that numbers are not easy to handle (I mean, how to remeber that string 0 is "mFile", string 1 is "mFilePrefs")... One solution is to use constants like this :
mFileStr = 0;
mFilePrefsStr = 1;
[...]
ShowMessage(MySoftStrings[BackupCompleteStr]);
An other problem is that the ini file might be quite hard to edit... So you may have to create a small translation soft.

You can, also take a look to the resourcestrings ;-)

Good luck :)

Regards
0
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!

 

Author Comment

by:Professor
ID: 9759878
Maybe we should do this via icq or something ... might be easier *lol* ;)
0
 
LVL 8

Accepted Solution

by:
gmayo earned 125 total points
ID: 9762239
Constants:

procedure tLang.Load;
var lngIni : TMemIniFile;
const
  SECTION = 'frmMain';
begin
  ...
  mFile := ReadString(SECTION,'mFile','');
etc

Geoff M.
0
 
LVL 2

Expert Comment

by:j42
ID: 9762338
ok, not tested:

type
  TMyTexts = (mFile, mFilePrefs, ..., mHelpUpdate);   // Note: No quotes
const
  MY_TEXTS: array[TMyTexts] of string = ('mFile', 'mFilePrefs', ..., 'mHelpUpdate');   // Quotes!
...
var
  myVars: array[TMyTexts] of string;
  i: TMyTexts;
begin
  ...
  for i := Low(i) to High(i) do
  begin
    myVars[i] := ReadString('frmMain', MY_TEXTS[i], '');
  end;

  // Example: LabelFile.Caption := myVars[mFile];
end;

To add a new item you have to update TMyTexts and MY_TEXTS.
0
 
LVL 8

Expert Comment

by:gmayo
ID: 10086773
Any good?
0

Featured Post

Independent Software Vendors: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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