Using the conditional operators (?:) in combination with properties in C++ builder shows a very strange behaviour.
When assigning a UnicodeString value with the value of a property it remains "NULL":
UnicodeString s2 = MyPropertyValue.IsEmpty() ? UnicodeString( "emptyValue" ) : MyPropertyValue;
See the example below:
Header:
class TForm1 : public TForm
{
__published:
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private:
UnicodeString FMyPropertyValue;
void SetMyPropertyValue( UnicodeString AValue ) {
FMyPropertyValue = AValue;
}
UnicodeString GetMyPropertyValue( void ) {
return FMyPropertyValue;
}
public:
__fastcall TForm1(TComponent* Owner);
__property UnicodeString MyPropertyValue = { read=GetMyPropertyValue, write=SetMyPropertyValue };
};
Cpp File:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner), FMyPropertyValue( "somevalue" ){}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s1 = MyPropertyValue;
UnicodeString s2 = MyPropertyValue.IsEmpty() ? UnicodeString( "emptyValue" ) : MyPropertyValue;
UnicodeString s3 = MyPropertyValue.IsEmpty() ? UnicodeString( "emptyValue" ) : FMyPropertyValue;
UnicodeString s4 = System::Sysutils::EmptyStr;
if ( MyPropertyValue.IsEmpty() )
{
s4 = UnicodeString( "emptyValue" );
}
else
{
s4 = MyPropertyValue;
}
}
While s1, s3 and s4 will be "someValue", s2 still remains "NULL".
Why?