Board index » cppbuilder » Publishing properties of type "class"

Publishing properties of type "class"

Hi All

I hope someone can help.

I am trying to build a component with a TriState properties (true, false and
Don'tCare). I've already defined the TriState type with all its operators
etc. and it works.

However when including this as a published property, the compiler disallows
the type.

According to the help files the type restrictions are:

"Properties defined in a __published section cannot be array properties. The
type of a property defined in a __published section must be an ordinal type,
a real type, a string type, a small set type, a class type, or a method
pointer type".  ---- a class type is O/K then????

I've striped the the code down to that below and and get the same compile
error.

If the type class is not allowed (independent of it's specific inclusion)
how is it, as AnsiString is a class, then the last line is accepted?

I am still using version 4.0

//==========================================================================
=
class TTest
{ public:
 int a ;

Quote
} ;

//--------------------------------------------------------------------------
-
//
class PACKAGE TMode : public TComponent
{
private:
public:
  TTest FVersion ;
  AnsiString FSVersion ;
//--------------------------------------------------------------------------
-
//
public:
__published:
 __property TTest Version   = { write = FVersion
                    , read = FVersion} ;
 __property AnsiString SVersion   = { write = FSVersion
                    , read = FSVersion} ;

Quote
};

jeffp...@agincourt.co.uk
 

Re:Publishing properties of type "class"


Quote
"Jeff Parr" <jeffp...@agincourt.co.uk> wrote in message

news:3c23de9a_1@dnews...

Quote
> I am trying to build a component with a TriState properties (true, false
and
> Don'tCare). I've already defined the TriState type with all its operators
> etc. and it works.

For the record, this belongs in
borland.public.cppbuilder.vcl.components.writing instead.

Quote
> __published:
>  __property TTest Version   = { write = FVersion, read = FVersion} ;

In order to publish a class, you should use a pointer instead (and since
__published only applies to the Object Inspector, derive the class from
TPersistent if you want it to be visible in the Object Inspector):

    class TTest : TPersistent
    {
    private
        int a;
    public:
        void __fastcall Assign(TPersistent *Source);
    __published:
        __property int A = { read = a, write = a };
    };

    void __fastcall TTest::Assign(TPersistent *Source)
    {
        TTest *test = dynamic_cast<TTest*>(Source);
        if(test)
            a = test->A;
        else
            TPersistent::Assign(Source);
    }

    class PACKAGE TMode : public TComponent
    {
    private:
        TTest *FVersion
        void __fastcall SetVersion(TTest *Value);
    public:
        __fastcall TMode(TComponent *Owner);
        __fastcall ~TMode();
    __published:
        __property TTest* Version = { read = FVersion, write = SetVersion };
    };

    __fastcall TMode::TMode(TComponent *Owner)
    {
        FVersion = new TTest;
    }

    __fastcall TMode::~TMode()
    {
        delete FVersion;
    }

    void __fastcall TMode::SetVersion(TTest *Value)
    {
        FVersion->Assign(Value);
        // do something in response to the change
    }

Quote
>  __property AnsiString SVersion   = { write = FSVersion, read = FSVersion}

;

AnsiString is a special-case class, the VCL knows how to handle it without
needing to use pointers.

To really accomplish what you're trying to do, you could just use an enum
instead of a class, ie:

    enum TriSta{*word*249}um {tsTrue, tsFalse, tsDontCare};

    class PACKAGE TMode : public TComponent
    {
    private:
        TriSta{*word*249}um FTriState;
        void __fastcall SetTriState(TriSta{*word*249}um Value);
    __published:
        __property TriSta{*word*249}um TriState = { read = FTriState, write =
SetTriState };
    };

    void __fastcall TMode::SetTriState(TriSta{*word*249}um Value)
    {
        if(FTriState != Value)
        {
            FTriState = Value;
            // do something in response to the change
        }
    }

Gambit

Other Threads