[DUG] Why use a set when a string will work OK (and less code)?

Myles Penlington myles at ams.co.nz
Thu May 10 11:18:01 NZST 2007


The advantage of enumerated types is strong compiler type checking. Ie
no spelling/keying mistakes.
If you use the normal convention for enumerated types, .e.g

MyStayus = (msActive, msPending, msEnded ) etc

Then you can use a function and RTTI to convert the enumerated type name
into a string for display. There is a RTTI function that returns the
name of an enumerated type e.g. "msActive", and then your function can
strip off the leading lowercase characters.

Myles.


-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Nick
Sent: Thursday, 10 May 2007 10:49
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Why use a set when a string will work OK (and less code)?

Why use a set when string work ok? (and I think it's a set)
Question: I have seen this quite a bit and apparently it's "good 
practice" however to me it seems like more work.
Example

Whats the point in doing this
MyStatus = (Active, Pending, Ended, Paused, Deleted, Suspended););
type
  Something = class
  name : string;
  status : MyStatus;
end;

then example on create or something
  status := Active;

(yes, missed out lots of steps :P)
and now if I want to show the status, I have to do something like..
If status = MyStatus(Active) then showmessage('Active');
If status = MyStatus(Pending) then showmessage('Pending');  etc

Why go though all that hassle when just doing this is fine
 status : string;
 status := 'Active'
 showmessage(status);

so instead of using "MyStatus" types I just use a string - this means 
when I want to output the status to the user I don't have to try and 
convert it to string first.
So, why do it?

Hmmmm
Nick
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
unsubscribe

Attention:
This communication is confidential and may be legally privileged.  If you are not the intended recipient, please do not use, disclose, copy or distribute it, other than to return it to us with your confirmation that it has been deleted from your system.



More information about the Delphi mailing list