|
|
|
start date: Wed, 15 Aug 2007 14:54:00 -0700,
posted on: microsoft.public.dotnet.languages.vc
back
| Thread Index |
|
1
Nick am
|
|
2
David Lowndes lid
|
|
3
Ben Voigt [C++ MVP] am
|
|
4
Nick am
|
|
5
David Lowndes lid
|
|
6
(Jeffrey Tan[MSFT])
|
Beginner C++/CLI questions
Hello,
I've got what I'm sure are some dumb questions. I've got some existing C++
code that I want to move to a C++/CLI assembly to make it easier to use in a
..NET app as well as continuing to use it in an existing C++ app. It's simple
code, but I'm not sure if I'm setting up my function definitions properly.
If I've got a function that takes a string would this be correct:
void PassInString(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.
Thanks for any help, I really appreciate it.
Thanks,
Nick
Date:Wed, 15 Aug 2007 14:54:00 -0700
Author:
|
Re: Beginner C++/CLI questions
>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
Date:Wed, 15 Aug 2007 23:19:41 +0100
Author:
|
Re: Beginner C++/CLI questions
"David Lowndes" <DavidL@example.invalid> wrote in message
news:nru6c3d1gk90d6at1tbo6llj8s2gfsdntj@4ax.com...
> >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
>>errors.
>
> String ^ fn()
> {
> String ^ s = gcnew String( "Whatever" );
> return s;
> }
>
> Dave
Date:Thu, 16 Aug 2007 08:27:20 -0500
Author:
|
Re: Beginner C++/CLI questions
Thanks Dave, I really appreciate it. Out of curiousity, what does the % do?
Anything?
Thanks,
Nick
Date:Thu, 16 Aug 2007 18:21:52 -0700
Author:
|
Re: Beginner C++/CLI questions
>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
Date:Fri, 17 Aug 2007 07:50:43 +0100
Author:
|
Re: Beginner C++/CLI questions
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.
Date:Fri, 17 Aug 2007 07:27:06 GMT
Author:
|
|
|