Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2007

Tip: Looking for answers? Try searching our database.

Beginner C++/CLI questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick - 15 Aug 2007 22:54 GMT
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
David Lowndes - 15 Aug 2007 23:19 GMT
>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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.