[DUG] Why use a set when a string will work OK (and less code)?
Conor Boyd
Conor.Boyd at trimble.co.nz
Thu May 10 11:31:51 NZST 2007
Yes, or if you want more control over your descriptive values, then you
can do something like this:
TGalleryStatus = (gsUnknown, gsSuccess, gsMajVerInvalid,
gsMinorVerInvalid, gsVerFmtInvalid, gsVerMissing, gsPasswordWrong,
gsLoginMissing, gsUnknownCommand, gsNoAddPermission,
gsNoFilename, gsUploadFailed,
gsNoWritePermission, gsNoCreateAlbumPermission,
gsCreateAlbumFailed, gsURLNotFound404Error,
gsImageNotLoaded, gsNetworkError,
gsNoWriteableAlbums, gsOffline, gsNoViewPermission,
gsMoveAlbumFailed, gsRotateImageFailed);
TGalleryStatusDescriptions: array
[Low(TGalleryStatus)..High(TGalleryStatus)] of String =
('Unknown response or other failure.',
'The command the client sent in the request completed successfully.
The data (if any) in the response should be considered valid.',
'The protocol major version the client is using is not supported.',
'The protocol minor version the client is using is not supported.',
'The format of the protocol version string the client sent in the
request is invalid.',
'The request did not contain the required protocol_version key.',
'The password and/or username the client sent in the request is
invalid.',
'The client used the login command in the request but failed to
include either the username or password (or both) in the request.',
'The value of the cmd key is not valid.',
'The user does not have permission to add an item to the gallery.',
'No filename was specified.',
'The file was received, but could not be processed or added to the
album.',
'No write permission to destination album.',
'A new album could not be created because the user does not have
permission to do so.',
'A new album could not be created, for a different reason (name
conflict).',
'Requested Gallery2 URL/command/module not found (404 error).',
'Image not loaded.',
'Network/socket error.',
'No albums writeable by the current user exist in this Gallery.',
'GLoSS is offline.',
'No view permission for this image.',
'The album could not be moved',
'The image could not be rotated.');
Which, as Myles says, gives me strong compiler checking, and means that
I can rely on there always being a description for a particular set
member.
Cheers,
C.
-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Myles Penlington
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.
-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Nick
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?
More information about the Delphi
mailing list