Board index » cppbuilder » Assigning TStrings* to TStringList property

Assigning TStrings* to TStringList property

Hi,
I've encountered this problem several times and still don't know a solution:

I have a class that manages a TStringList property, f.e.

class Members
{
  private:
     bool changed;
     TStringList* FNames;

     void __fastcall SetNames(TStringList* newNames);
     ...

  public:
     ...

     __property TStringList* Names = {read=FNames, write=SetNames};

     ...

Quote
};

void __fastcall Members::SetNames(TStringList* newNames)
{
   FNames->Assign(newNames);
   changed = true;

Quote
}

Now if my main form has a TListBox (listBox) and an instance variable of
Members (members)
what I want to do then in the main form is:

members->Names = listBox->Items;

The compiler complains "Cannot convert 'TStrings* const' to 'TStringList*' "
I know that TStringList::Assign accepts TStrings* but the setter method does
not.

What should I do?

thanks,
Nick

 

Re:Assigning TStrings* to TStringList property


Change your Names property to use TStrings* instead of TStringList* (you can
still use TStringList internally in your class).  Also use a getter function
to keep the compiler happy, since TStringList* doesn't match TStrings* when
you access FNames directly from the property read specifier:

    class Members
    {
    private:
        TStringList* FNames;

        TStrings* __fastcall GetNames();
        void __fastcall SetNames(TStrings* newNames);

    public:
        __property TStrings* Names = {read=GetNames, write=SetNames};
    };

    TStrings* __fastcall Members::GetNames()
    {
        return FNames;
    }

    void __fastcall Members::SetNames(TStrings* newNames)
    {
        FNames->Assign(newNames);
    }

Gambit

Quote
"nicolasr" <nicol...@gmx.net> wrote in message

news:3d66c084$1@newsgroups.borland.com...
Quote
> Now if my main form has a TListBox (listBox)
> and an instance variable of Members (members)
> what I want to do then in the main form is:

> members->Names = listBox->Items;

> The compiler complains "Cannot convert 'TStrings* const'
> to 'TStringList*' "
> I know that TStringList::Assign accepts TStrings* but the
> setter method does not.

Re:Assigning TStrings* to TStringList property


thanks, it works!
Nick

Other Threads