473,326 Members | 2,655 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,326 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 2683
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.