Solved

Any Simplification possible?

Posted on 2003-11-16
7
231 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
Technology Partners: 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

632 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