473,503 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Arguments - Reference to a Value type?

Another noob question for you all . . .

I have functions written in C++ that I want to call from C#. I need to
be able to pass a reference to a value type (like an int) so that the
function can change the value - just like passing a pointer to an int
in good old unmanaged C.

Here is the function in C++ (member of testclass) - this compiles to a
DLL without error in C++:

int test_function(int arg1, int^ arg2)
{
int result;

// arg1 is a VALUE type passed IN to the function. The
// function can use the value but not change it.

// arg2 is a REFERENCE to a value type, so the function
// should be able to use the value and change the value.

result = arg1 + *arg2;
*arg2 = 1234;

return( result );
}
I use the DLL from C# so I can call the function.
Here is what I am doing in C# (doesn't compile):

int status, val1, val2;

val1 = 11;
val2 = 12;
status = testclass.test_function(val1, ref val2); //
Chokes on "ref arg2"

The error says - Argument '2': cannot convert from 'ref int' to
'System.ValueType'.

I'm sure I'm doing something fundamentally wrong here. Can someone
here set me straight?

Thanks.

Nov 13 '06 #1
3 5419
Rich wrote:
I have functions written in C++ that I want to call from C#. I need to
be able to pass a reference to a value type (like an int) so that the
function can change the value - just like passing a pointer to an int
in good old unmanaged C.

Here is the function in C++ (member of testclass) - this compiles to a
DLL without error in C++:

int test_function(int arg1, int^ arg2)
{
int result;

// arg1 is a VALUE type passed IN to the function. The
// function can use the value but not change it.

// arg2 is a REFERENCE to a value type, so the function
// should be able to use the value and change the value.

result = arg1 + *arg2;
*arg2 = 1234;

return( result );
}
I use the DLL from C# so I can call the function.
Here is what I am doing in C# (doesn't compile):

int status, val1, val2;

val1 = 11;
val2 = 12;
status = testclass.test_function(val1, ref val2); //
Chokes on "ref arg2"

The error says - Argument '2': cannot convert from 'ref int' to
'System.ValueType'.

I'm sure I'm doing something fundamentally wrong here. Can someone
here set me straight?
If you want to pass this as a reference from C# to C++, the define your C++
method as follows:

int test_function(int arg1, int % arg2)
{
int result;

result = arg1 + arg2;
arg2 = 1234;

return (result);
}

^ tells managed C++ you want a handle to an object on the GC heap. % tells
managed C++ you want a tracking reference.
--
Tom Porterfield

Nov 13 '06 #2

Perfect - you hit the nail on the head! That solved the problem -
Thanks!

Nov 13 '06 #3
Rich wrote:
Perfect - you hit the nail on the head! That solved the problem -
Thanks!
Glad to help.
--
Tom Porterfield
Nov 13 '06 #4

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

Similar topics

9
4935
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
10801
by: beavenour | last post by:
I am looking for some help creating a more universal function out of this lame attempt at javascript. I have little experience with javascript and want to be able to universalize this function. ...
15
4177
by: chirs | last post by:
I am trying to understand a piece of code. In a javascrpit file, there is a function: function ItemStyle(){ var names=; addProps(this,arguments,names,true); }; In the html file, it calls...
3
14906
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) {...
2
7660
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
39
7591
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...
10
2274
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
1
1793
by: cognominal | last post by:
I am using fifrefox 1.5.0.2 and I have modified the javascript shell to deal with e4x. Below is a session. I would have expected <toto/>.nodeKind to be of type "function". Is this right? Is...
28
4272
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
14
1527
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the...
0
7202
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
7280
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,...
1
6991
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
7460
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...
1
5014
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...
0
4672
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...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
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...

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.