Talk:String Constants

From RAD Studio
Jump to: navigation, search

I was struggling to find an example that was clear to me (As a beginning C++ Programmer) so I could use Strings with the 'Caption' on a Panel. I couldn't find anything that seemed to do what I wanted it to do, so I created the following small routine which declared three character strings and added them to a panel->Caption object. It does it on FormCreate so that the data is available to be displayed immediately on startup. TForm1 is the main form. Panels 1-3 are showing what the value of each character string contains. Panel 4 displays the concatenated strings after they have been added together. It adds the first item (With StrCpy) then concatenates the other strings onto the end of the first copied string.

void __fastcall TForm1::FormCreate(TObject *Sender)
{
	char *char1 = "A";
	char *char2 = "B";
	char *char3 = "C";

	TForm1::Panel1->Caption = char1;
	TForm1::Panel2->Caption = char2;
	TForm1::Panel3->Caption = char3;

	char* szBuffer = new char[50];

	StrCopy(szBuffer, char1);
	StrCat(szBuffer,  char2);
	StrCat(szBuffer,  char3);
	StrCat(szBuffer, " reversed is ");
	StrCat(szBuffer,  char3);
	StrCat(szBuffer,  char2);
	StrCat(szBuffer,  char1);
	TForm1::Panel4->Caption = szBuffer;
}

This example outputs the following text to the TForm1::Panel4->Caption object;

"ABC reversed is CBA"

Response

Hi Andy -

Your code example is well appreciated, and we are trying to get it to run in RAD Studio. Currently we are seeing [BCC32 Error] Unit2Bngo.cpp(14): E2316 '_fastcall TForm2::FormCreate(TObject *)' is not a member of 'TForm2'.

After we get your example running error-free, we will try to incorporate it into the documentation, perhaps as a code example linked to this String Constants page.

Many thanks for your contribution to the docwiki!

KrisHouser 15:03, 30 April 2012 (PDT)

RAD-10776