Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Make user-defined types persistent

Posted on 1998-12-10
9
Medium Priority
?
131 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 400 total points
ID: 1350325
ok there is a posted answer

meikl
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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