473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Passing parameters by reference

How can I pass a reference type (a String) by reference from a c# class to a
method in a managed C++ program.
I found I could create a method in C++ as
MyMethod(String^& str)
{
.....modify str sp caller sees change.
}
and call it from another function in C++. One document said I should use
the ref keyword to call from c# ( such as MyMethod(ret str) ). When I do I
get a compile error saying can't convert from ref String to String*.

I know there must be some way.
Clyde

Jun 3 '07 #1
3 2685
ClydeL wrote:
How can I pass a reference type (a String) by reference from a c# class to a
method in a managed C++ program.
I found I could create a method in C++ as
MyMethod(String^& str)
{
.....modify str sp caller sees change.
}
and call it from another function in C++. One document said I should use
the ref keyword to call from c# ( such as MyMethod(ret str) ). When I do I
get a compile error saying can't convert from ref String to String*.

I know there must be some way.
If it is 2005/8.0 then try:

MyMethod(String^ % str)
{

Arne
Jun 3 '07 #2
Thank you very much.
I had tried somethings with tracking references, just not the correct one.

Clyde

"Arne Vajhøj" wrote:
ClydeL wrote:
How can I pass a reference type (a String) by reference from a c# class to a
method in a managed C++ program.
I found I could create a method in C++ as
MyMethod(String^& str)
{
.....modify str sp caller sees change.
}
and call it from another function in C++. One document said I should use
the ref keyword to call from c# ( such as MyMethod(ret str) ). When I do I
get a compile error saying can't convert from ref String to String*.

I know there must be some way.

If it is 2005/8.0 then try:

MyMethod(String^ % str)
{

Arne
Jun 4 '07 #3
ClydeL wrote:
How can I pass a reference type (a String) by reference from a c# class to a
method in a managed C++ program.
I found I could create a method in C++ as
MyMethod(String^& str)
{
.....modify str sp caller sees change.
}
and call it from another function in C++. One document said I should use
the ref keyword to call from c# ( such as MyMethod(ret str) ). When I do I
get a compile error saying can't convert from ref String to String*.

I know there must be some way.
Clyde
If you only have one object that you want to change, then it's better to
return it from the method:

string MyMethod(string str) {
str = str.ToLower();
return str;
}

string someString = "Hello world";
someString = MyMethod(someString);
If you want to call an existing method that uses a reference, use the
ref keyword:

string someString = "Hello world";
MyMethod(ref someString);

--
Göran Andersson
_____
http://www.guffa.com
Jun 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.