String object as function argument

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m not sure where to post this, so I post it here. It’s more like a VC++.NET
/ Managed Code problem. Probably easy to fix for you guys.

I want to use a String object as a parameter for a function. Here is what it
should look like:

String *a = "Hallo";
this->SomeClass->FormatString(a);
this->label->Text = a;

The FormatString function looks like this:

void FormatString(String *b)
{
b = “Alohaâ€;
}


What comes up in the label is “Hallo†instead of “Alohaâ€. All class
declarations and everything works fine. The code compiles without error or
warning, but I’m not able to pass the String object to “FormatString†in a
way that it can change it. What am I doing wrong?
 
cnickl said:
I’m not sure where to post this, so I post it here. It’s more like a VC++.NET
/ Managed Code problem. Probably easy to fix for you guys.

I want to use a String object as a parameter for a function. Here is what it
should look like:

String *a = "Hallo";
this->SomeClass->FormatString(a);
this->label->Text = a;

The FormatString function looks like this:

void FormatString(String *b)
{
b = “Aloha”;
}


What comes up in the label is “Hallo” instead of “Aloha”. All class
declarations and everything works fine. The code compiles without error or
warning, but I’m not able to pass the String object to “FormatString” in a
way that it can change it. What am I doing wrong?

You need to pass the handle by reference, e.g. declare FormatString as
below:

void FormatString(String*& b)

Then "b" will simply be another name for the argument "a", and your
modification of "b" will actually modify "a".
 
i knew i had to pass it by reference, just couldn’t get the syntax right even
so it is that easy. Well what can i say. Thank you very much.
 
Back
Top