Passing parameters by reference

G

Guest

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
 
G

Guest

ClydeL said:
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
 
G

Guest

Thank you very much.
I had tried somethings with tracking references, just not the correct one.

Clyde
 
G

Guest

ClydeL said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top