473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2698
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(someSt ring);
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
14920
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
5
36391
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
26
45491
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 defined in "A.html", but every attempt results in an "is not a function" error. I have tried to invoke the function using...
39
7623
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
8
2111
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, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed...
13
2778
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
4404
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup...
6
5981
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 original value of the local." My code is: using System; using System.Collections.Generic;
10
3917
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication eventname string source string ID string
7
3293
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.