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

Sean Cross Sean.Cross at catalystrisk.co.nz
Thu May 10 11:12:06 NZST 2007


Because then you are forever checking status with code like


If Status = 'Active' then  ..
Else if Status = 'Pending'...

So you end up using consts.  And you still have the problem that your Status may end up as '' or 'Ative' or 'as323'

And you can't use case statements.

If status is purely informational, then using a string is fine.  Once you start checking it and acting on it, then string based code becomes increasingly ugly.

What I do is something like:

TMyStatus = (Active, Pending, Ended, Paused, Deleted, Suspended);
StatusMeanings = array[TMyStatus] of string = ('Active', 'Pending', 'Ended', 'Paused', 'Deleted', 'Suspended');
...
ShowMessage(StatusMeanings[Status]);

Regards

Sean Cross
IT Systems Development Manager

Catalyst Risk Management
PO Box 230
50 Dalton St
Napier 4140
DDI: 06-8340362
mobile: 021 270 3466
Visit us at http://www.catalystrisk.co.nz/

Offices in Auckland, Napier, Wellington & Christchurch

Disclaimer:
"The information contained in this document is confidential to the addressee(s) and may be legally privileged. Any view or opinions expressed are those of the author and may not be those of Catalyst Risk Management. No guarantee or representation is made that this communication is free of errors, viruses or interference. If you have received this e-mail message in error please delete it and notify me. Thank you."


> -----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 a.m.
> 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



More information about the Delphi mailing list