Mega Code Archive

 
Categories / Delphi / Strings
 

Boolean to String Conversion

Title: Boolean to String Conversion Question: How I convert boolean values to the words depending on the situation? For example, TRUE here means "Enabled", and FALSE there means "Failed"? Answer: Here is the code snippet to do the job. If the second parameter is omitted, the function returns "TRUE" or "FALSE". Modify the function declaration to change the default returning values. Expand TBooleanWordType and BooleanWord definitions to include more specific values if needed. interface [...] type TBooleanWordType = (bwTrue, bwYes, bwOn, bwEnabled, bwSuccessful, bwOK, bwOne); [...] function BoolToStr (AValue: boolean; ABooleanWordType: TBooleanWordType = bwTrue): string; [...] {=====================================================} implementation [...] const BooleanWord: array [boolean, TBooleanWordType] of string = ( ('FALSE', 'No', 'Off', 'Disabled', 'Failed', 'Cancel', '0'), ('TRUE', 'Yes', 'On', 'Enabled', 'Successful', 'OK', '1') ); [...] {-----------------------------------------------------} function BoolToStr (AValue: boolean; ABooleanWordType: TBooleanWordType = bwTrue): string; begin Result := BooleanWord [AValue, ABooleanWordType]; end; {--BoolToStr--} [...]