Solved

Any Simplification possible?

Posted on 2003-11-16
7
221 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

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

Accepted Solution

by:
gmayo earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Any good?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now