Solved

Make user-defined types persistent

Posted on 1998-12-10
9
128 Views
Last Modified: 2010-04-04
Say I've a user-defined type and variable of that type

type  TSize = (szUndefined, szSmall, szMedium, szLarge, szXLarge)

var   MySize : TSize

When the app shutsdown I'd like to save the value of MySize to an IniFile (or anywhere else) so I've got to convert  MySize to some simple type that IniFile recognizes e.g. an integer:

MySize := szMedium;
IniFile.WriteInteger('Whatever', 'Size', Ord(MySize));

creates a file of this format
[Whatever]
Size=1

The 'Ord' function makes this code very neat but there doesn't seem to be an elegant way of reading the value again, the best I can think of is :

case IniFile.ReadInteger('Whatever', 'Size', Ord(szUndefined)) of
   Ord(szSmall) : MySize := szSmall;
   Ord(szMedium) : MySize := szMedium;
   Ord(szLarge) : MySize := szLarge;
   Ord(szXLarge) : MySize := szXLarge;
end;

If TSize is ever extended the 'write' code will not need to change but the 'read' code will just get bigger.  I could declare szMedium etc. as integer constants but I think making them user-defined types enforces stronger type-checking so I'd like to stick with it.

Any suggestions?
0
Comment
Question by:andrewjackson
[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
  • 6
  • 3
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350317
hi andrewjackson,

try by read

MySize := TSize(IniFile.ReadInteger('Whatever', 'Size', Ord(szUndefined)));

meikl

0
 

Author Comment

by:andrewjackson
ID: 1350318
meikl,

So easy, why didn't I think of that!

There is still one potential problem though which I can't seem to workaround.  I want to handle the error condition where the integer value stored in the the ini file is out of bounds.  I'd have expected Delphi to raise an exception in this case but it doesn't.  I've written the following routine..

procedure TForm1.Button1Click(Sender: TObject);
var
   MySize : TSize;
   IniFile : TIniFile;
begin
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
   IniFile := TIniFile.Create('test.ini');
   try
     MySize := TSize(IniFile.ReadInteger('Whatever', 'Size', Ord(szUndefined)));
     ShowMessage(MySize is ' + IntToStr(Ord(MySize)))
   finally
     Inifile.Free;
   end;
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
end;


Range and overflow checking are both on but if the 'Size' field in the ini file is set to say 10 then no exception is generated.  If I inspect the value of MySize after the assignment the debugger displays

'MySize = (out of bound) 10'

I could add my own check :

if (Ord(MySize) < Ord(szUndefined)) or
   (Ord(MySize) > Ord(szXLarge)) then
  begin
    ERROR
  end

but again this check would need to be updated if TSize is ever extended.

Any suggestions?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350319
hi andrewjackson,

try after reading

if not(MySize in TSize) then ...

meikl
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:andrewjackson
ID: 1350320
No I've already tried that and it won't compile.

I've also tried High and Low but these aren't valid for user defined types.

Andrew
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350321
hi andrewjackson,

you've right,

but this should work

if not(MySize in [szUndefined..szXLarge]) then ..

of course you can new values insert only between this two.
Not the best solution, i will look to a better one

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350322
hi andrewjackson,

you've right,

but this should work

if not(MySize in [szUndefined..szXLarge]) then ..

of course you can new values insert only between this two.
Not the best solution, i will look to a better one

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350323
hi andrewjackson,

now i have it,

    if (ord(MySize) < ord(low(TSize))) or (ord(MySize) > ord(high(TSize))) then ..{out of bounds}

meikl
0
 

Author Comment

by:andrewjackson
ID: 1350324
Thanks, that it now.

I want to give you the points but you've only posted a comment not proposed an answer so I'm not sure how to close this thread and give you the reward.

Any ideas?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 1350325
ok there is a posted answer

meikl
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run video youtube webbrowse 10 86
select query - oracle 16 103
Tvertscrollbox like a whatsapp layout 5 69
Using MMsystem To change audio input 1 40
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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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