Solved

Make user-defined types persistent

Posted on 1998-12-10
9
127 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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