How a function can modify an AnsiString passed to her (and how i can pass AnsiStrings to functions)?

Someone can give an example? Im using borland c++ 5.0

Recommended Answers

All 7 Replies

An AnsiString is a class, just like any other type of data.

To modify it in a function, pass it by reference: void myFunction( AnsiString &s ) Hope this helps.

Any help is welcome, but... Why this works:

void TForm1::AnsiChange(AnsiString *stringtobechanged)
{
        *stringtobechanged = (AnsiString)"test";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString TestString;
        AnsiChange(&TestString);
        Button1->Caption = TestString;
}

And why this don't work:

void TForm1::AnsiChange(AnsiString *stringtobechanged)
{
        *stringtobechanged = (AnsiString)"test";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiChange(&(Button1->Caption));
}

The only possible reason I can think of off the top of my head is that Button1->Caption is not an AnsiString.

In the Borland Help says:

TButtonControl::Caption

Specifies the caption that appears on the button.

__property AnsiString Caption = {read=GetText, write=SetText, default=false};

Caption is not an AnsiString, it is a __property.

A property is a way of making a member field that looks like a regular variable but which actually uses functions to get and set the value. C++ doesn't have properties in the language, but it is not uncommon to write a little class which does the job.

The __property class overloads all the operators necessary to make it look like an AnsiString, but it is still a different class, so you cannot pass it directly to the function. Since you don't know anything about the string besides what the accessor function gives you, you must use a temporary. This is the achilles heel of properties.

AnsiString temp;
  temp = Button1->Caption;
  AnsiChange( &temp );
  Button1->Caption = temp;

Enjoy.

I know the thread is old, but I can't help writing a little additional note, since I just encountered a similar problem wanting to swap Captions. After reading Duoas's answer about properties, I was not happy with the mess this temporary would give in my code (isn't one major reason for creating functions in the first place the wish to avoid mess? ;)
So instead, I settled for a different solution: passing the control by reference and using a temporary inside the function (for swapping). And the above function would not even need a temporary:

void TForm1::BtnCaptionChange(TButton *buttontobechanged)
{
        buttontobechanged->Caption = (AnsiString)"test";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        BtnCaptionChange(Button1);
}

(Since Button1 is already a pointer, the code looks nicer as well.)
Unfortunately, it seems not possible to generalise this function to modify the Caption property of any TObject*, since Caption was not accessible to the function. (Hence, the name BtnCaptionChange and not just CaptionChange)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.