Solved

Any Simplification possible?

Posted on 2003-11-16
7
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 76
control image tags in a string ? 12 169
Firemonkey allowing RTL on android 6 77
Delphi, TImage, Png 2 56
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

738 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