Solved

Make user-defined types persistent

Posted on 1998-12-10
9
130 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

617 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