![]() |
BoolToStr improved
Es gibt immer wieder Situationen, in denen man einen boolschen Wert nicht in True/False bzw 0/-1 sondern in Ja/Nein übersetzen muss. Bislang bietet Delphi dafür keine passende Funktion ... bislang:
Delphi-Quellcode:
Beispiele:
function BoolToStr(aValue: Boolean; const aYes: string = 'Yes'; const aNo: string = 'No'): string; overload;
function BoolToStr(aValue: Boolean; const aYes: string = 'Yes'; const aNo: string = 'No'): string; begin if aValue then Result := aYes else Result := aNo end;
Delphi-Quellcode:
würde "Yes" zurück liefern,
BoolToStr(True)
Delphi-Quellcode:
würde "Nein" zurück liefern
BoolToStr(False, "Ja", "Nein")
|
AW: BoolToStr improved
Sind dafür nicht TrueBoolStrs und FalseBoolStrs gedacht?
|
AW: BoolToStr improved
Gibts nicht in der Unit Math auch ein IfThen() für Strings? - Das hat das dann schon alles drauf, heißt nur anders.
Bernhard |
AW: BoolToStr improved
[add]
@rollstuhlfahrer: in Math ist das IfThen für Integer und Extended ... das IfThen für Strings gammelt in der StrUtils rum. @uligerhardt: jupp (wenn man nebenbei mit seinem Cheff die letzten Sachen für den Arbeitsvertrag macht, dann braucht man sich nicht wundern, wenn viele schon vorher geantwortet haben) [/add] Jupp, in Delphi fehlt dieses leider. :cry: Wobei man Delphi dieses aber beibringen könnte, indem man ![]() ![]() BoolToStr nimmt dabei die Werte im Index 0.
Delphi-Quellcode:
Allerdings wird BoolToStr an vielen Stellen in Delphi verwendet, weswegen man den Index 0 besser nicht verändern sollte.
//SetLength(TrueBoolStrs, 2);
//TrueBoolStrs[0] := 'yes'; //TrueBoolStrs[1] := DefaultTrueBoolStr; //SetLength(FalseBoolStrs, 2); //FalseBoolStrs[0] := 'no'; //FalseBoolStrs[1] := DefaultFalseBoolStr; VerifyBoolStrArray; i := Length(TrueBoolStrs); SetLength(TrueBoolStrs, i + 1); TrueBoolStrs[i] := TrueBoolStrs[0]; TrueBoolStrs[0] := 'yes'; i := Length(FalseBoolStrs); SetLength(FalseBoolStrs, i + 1); FalseBoolStrs[i] := FalseBoolStrs[0]; FalseBoolStrs[0] := 'no'; S := BoolToStr(B, True); B := StrToBool(S); Es bleibt also noch das andere Ende der Array übrig.
Delphi-Quellcode:
//VerifyBoolStrArray;
//SetLength(TrueBoolStrs, 2); //TrueBoolStrs[1] := 'yes'; //SetLength(FalseBoolStrs, 2); //FalseBoolStrs[1] := 'no'; VerifyBoolStrArray; i := Length(TrueBoolStrs); SetLength(TrueBoolStrs, i + 1); TrueBoolStrs[i] := 'yes'; i := Length(FalseBoolStrs); SetLength(FalseBoolStrs, i + 1); FalseBoolStrs[i] := 'no'; const MyTrueFalse: array[Boolean] of string = ('no', 'yes'); S := IfThen(B, 'yes', False); S := MyTrueFalse[B]; B := StrToBool(S); { IfThen siehe Unit StrUtils } PS: Aber du hast dabei auch die Umkehrfunktion vergessen, also für das StrToBool.
Delphi-Quellcode:
procedure AddBoolStr(const aYes, aNo: string);
var i: Integer; begin VerifyBoolStrArray; i := Length(TrueBoolStrs); SetLength(TrueBoolStrs, i + 1); TrueBoolStrs[i] := aYes; i := Length(FalseBoolStrs); SetLength(FalseBoolStrs, i + 1); FalseBoolStrs[i] := aNo; end |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz