0

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?

Herwig
  • 316
  • 1
  • 17
  • It seems that casting MyPropertyValue to (UnicodeString)MyPropertyValue solves the problem. – Herwig Dec 12 '17 at 15:13
  • Which version of C++Builder are you using, which compiler are you using - bcc32 (classic Borland 32bit), bcc32c (Clang 32bit), or bcc64 (64bit) - and which platform are you compiling for? – Remy Lebeau Dec 12 '17 at 20:38
  • Probably its some bug in compiler. I have today run into two problems related to class properties. First is some undocumented feature of compiler and second generates "internal code generator error" even if code is technically correct. I am using C++ Builder XE8, 32-bit classic compiler. – truthseeker Dec 13 '17 at 13:13
  • @truthseeker this is most likely not the case for newer compilers but for BDS2006 (I am stuck with and more or less happy) this [bds 2006 C hidden memory manager conflicts](https://stackoverflow.com/a/18016392/2521214) solved a lot of issues related to class/struct compilation and compiler errors. But it is wort a try (just adding 4 lines per class/struct). I would like to know if it helped or not. From some other answers responses this also helped in BCB6. I never saw any problems like this in BCB5 however. – Spektre Dec 14 '17 at 08:32
  • @RemyLebeau: The Version I am using is 10 (Seattle) and I used the classic compiler (32bit) for Windows Platform – Herwig Apr 22 '18 at 14:25

0 Answers0