AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt New Tip: TBooleanHelper.ToString and TUseBoolStrs enumeration
Thema durchsuchen
Ansicht
Themen-Optionen

New Tip: TBooleanHelper.ToString and TUseBoolStrs enumeration

Ein Thema von DP News-Robot · begonnen am 7. Okt 2019
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
14.985 Beiträge
 
#1

New Tip: TBooleanHelper.ToString and TUseBoolStrs enumeration

  Alt 7. Okt 2019, 17:20
Among the native type record helpers that were introduced in the Delphi RTL a few years back, there is one for the Boolean data type. It has a few methods you can apply to a Boolean variable, including ToString. This is the class helper definition:



If you call ToString on a Boolean variable you get 0 or -1 depending on the value. In other words,*True.ToString produces*-1.

The ToString method has also a second parameter,*UseBoolStrs, to let the method return a string with the text False or True. *However, if you call*True.ToString(True) you still get*-1. What's*up? As you can see in the definition above, there are*both a regular instance method and a class method called ToString that can be called with one Boolean parameter. The compiler picks the one without default parameter as best match.*

So the question is, how can I get the desired string output? Simply by using the*TUseBoolStrs enumeration. In fact, originally the signature of the methods was using a Boolean as parameter, that is the values False or True. More recently, this was changed to use a*TUseBoolStrs enumerator, with identical values False or True. In this way, the existing code compiles. The advantage of this trick you can specify the full value of the parameter, using the type as prefix and write:

True.ToString(TUseBoolStrs.True) This returns the string True, which is what we wanted in the first place. It it not an obvious solution, but it allows keeping the existing code compatible while addressing a design limitation (the two methods with the same name) that was already there. The only problem*is that few Delphi developers realize this -- I was ignoring*the solution until recently -- and this is why I just wrote this blog post!



Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:39 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz