Welcome, Guest
Username: Password: Remember me

Unicode sprintf

Posts 1 to 4 of 4
  • Oliver
  • Oliver's Avatar
  • OFFLINE
  • Gold User
  • No rank
5 days 4 hours ago #8948
I am stomped by a strange behaviour of sprintf when applied to the "Caption" property of a form.
The following code works as expected:
UnicodeString temp;
temp.sprintf (L"123");
Form1->Caption = temp;
However, this simplified version doesn't (the value of "Caption" doesn't change):
Form1->Caption.sprintf (L"123");
BTW the following also works:

Form1->Caption = Form1->Caption.sprintf (L"123");
Since Caption is a Unicode string, I don't understand why the 2nd version wouldn't work. What am I missing here?

Log in to reply.
  • Roger Cigol
  • Roger Cigol's Avatar
  • OFFLINE
  • Gold User
  • Rank: 33
  • Points: 85
  • Thanks 23
3 days 7 hours ago #8984
Hi Oliver,
The statement
Form1->Caption = temp;
assigns the value of temp to the PROPERTY called Caption in the class Form1.
Under the cover this assignment can (and usually does) hide a lot of other
necessary activity (typically by actually calling a WriteTheProperty() [my name] method).
In your particular case updating the property will also ensure that the painting
of the form on the screen also updates the caption.
Your example that doesn't work:
Form1->Caption.sprintf (L"123");
will make the actual string Form1->Caption equal to "123" but, because it doesn't
use the "=" notation it doesn't call the property WriteTheProperty() method so the
form displayed caption fails to update.
For more information (and perhaps a better explanation!) search for

__property

in the Embarcadero docwiki
Regards, Roger
Log in to reply.
  • Oliver
  • Oliver's Avatar
  • OFFLINE
  • Gold User
  • No rank
3 days 10 minutes ago #9032
Your example that doesn't work:
Form1->Caption.sprintf (L"123");will make the actual string Form1->Caption equal to "123" but,
Initially I thought the same, but the string never gets set. I checked with the debugger, but also using a TEdit field and adding that code:

Form1->Caption.sprintf (L"123");
Edit1->Text = Form1->Caption;
I do understand the Getter/Setter concept, but this still seems odd to me (should the sprintf in this case even be available?)
Log in to reply.
2 days 22 hours ago #9053
sprintf only returns the value. You must set it yourself.
Form1->Caption = Form1->Caption.sprintf(L"123");
Log in to reply.
Moderators: Carlos Perez