Solved

Any Simplification possible?

Posted on 2003-11-16
7
222 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now