>If I've got a function that takes a string would this be correct:
>
>void PassInString(String^% string1);
You only need String ^ string1
>What about when I need to return a string from a function. Could someone
>show me a simple code snippet? I've tried some things, but I keep getting
>errors.
String ^ fn()
{
String ^ s = gcnew String( "Whatever" );
return s;
}
Dave
Ben Voigt [C++ MVP] - 16 Aug 2007 14:27 GMT
> >If I've got a function that takes a string would this be correct:
>>
>>void PassInString(String^% string1);
This is equivalent to C#
void PassInString(ref String string1) { ... }
or
void PassInString(out String string1) { ... }
You would use System.Runtime.InteropServices.OutAttribute to specify the
latter.
> You only need String ^ string1
This is normal pass-by-value of a handle to an immutable string -- the
caller's copy cannot be changed.
>>What about when I need to return a string from a function. Could someone
>>show me a simple code snippet? I've tried some things, but I keep getting
[quoted text clipped - 7 lines]
>
> Dave
Nick - 17 Aug 2007 02:21 GMT
Thanks Dave, I really appreciate it. Out of curiousity, what does the % do?
Anything?
Thanks,
Nick
David Lowndes - 17 Aug 2007 07:50 GMT
>Thanks Dave, I really appreciate it. Out of curiousity, what does the % do?
>Anything?
Oh yes. It signifies a "tracking reference" - have a look on MSDN for
the details.
Dave
Jeffrey Tan[MSFT] - 17 Aug 2007 08:27 GMT
Hi,
I think "Ben Voigt [C++ MVP]" has explained the meaning of "%" in function
call. It means passing the CLR types by reference with tracking references.
Actually, there are 2 ways to return the result to the caller:
1. Through the return value.
2. Through the parameter by reference.(Using "%" in function parameter)
The code below demonstrates both 2 approaches:
void fnRef(String^% str)
{
str = str + "Whatever";
}
String ^ fn(String^ str)
{
String ^ s = gcnew String( str + "Whatever" );
return s;
}
int main(array<System::String ^> ^args)
{
String ^ str="abc";
String ^ result = fn(str);
Console::WriteLine(result);
fnRef(result);
Console::WriteLine(result);
return 0;
}
Please refer to the link below for several usage of "%" in C++/CLI:
http://msdn2.microsoft.com/en-us/library/8903062a(VS.80).aspx
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.