Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass By Reference in C#
#1
Hi Gintaras,

Does QM supports passing by reference using C#?

Example:
Macro PassByRef
Code:
Copy      Help
out
int a=1
int b=2
out "a=%i, b=%i" a b
CsFunc("" &a &b)
err
,out _error.description
out "a=%i, b=%i" a b

#ret
using System;

public class Test
{
,static public void PassByRef(ref int x, ref int y)
,{
,,x = 100;
,,y = 200;
,}
}
I've got this error:
a=1, b=2
0x80004002, No such interface supported.
Invalid cast from 'System.Int32' to 'System.Int32&'.
a=1, b=2


public (`fq)class(`) Test
{
(`i),(`)static public void PassByRef(`p)((`)(`fq)ref(`) (`t)int(`) x(`p),(`) (`fq)ref(`) (`t)int(`) y(`p))(`)
(`i),(`){
(`i),,(`)x (`o)=(`) (`n)100(`)(`p);(`)
(`i),,(`)y (`o)=(`) (`n)200(`)(`p);(`)
(`i),(`)}
}[/code]
#2
CsFunc doesn't.
Create object and call functions through IDispatch.

Macro Macro2527
Code:
Copy      Help
out
int a=1
int b=2
out "a=%i, b=%i" a b
CsScript x.AddCode("")
IDispatch d=x.CreateObject("Test")
d.PassByRef(&a &b)
err
,out _error.description
out "a=%i, b=%i" a b

#ret
using System;

public class Test
{
,public void PassByRef(ref int x, ref int y)
,{
,,x = 100;
,,y = 200;
,}
}
#3
It works great.
Thank You!
#4
Hi Gintaras,
The example above works with integer type when I change to "string" type I've got an error of "Error in PassByRefStr: type mismatch."
You know what's wrong with it?
Macro PassByRefStr
Code:
Copy      Help
out
str s1="s1"
str s2="s2"
out "s1=%s, s2=%s" s1 s2
CsScript x.AddCode("")
IDispatch d=x.CreateObject("Test")
d.PassByRefStr(&s1 &s2)
err
,out _error.description
out "s1=%s, s2=%s" s1 s2

#ret
using System;

public class Test
{
,public static void PassByRefStr(ref string x, ref string y)
,{
,,x = "s1_string";
,,y = "s2_string";
,}
}
#5
Need BSTR.

Macro Macro2527
Code:
Copy      Help
out
int a=1
str b="one"
out F"a={a}, b={b}"
CsScript x.AddCode("")
IDispatch d=x.CreateObject("Test")
BSTR _b=b
d.PassByRef(&a &_b)
b=_b
out F"a={a}, b={b}"

#ret
using System;

public class Test
{
,public void PassByRef(ref int x, ref string y)
,{
,,x = 100;
,,y = "two";
,}
}
#6
Thanks for quick solution.
It works like a charm!
#7
Hi Gintaras,
Sorry to bother you again, but I've got this error "unknown member" for the following code, and I don't know why?
Macro StringTest
Code:
Copy      Help
out
str s1="s1"
str s2="s2"
str s3="s3"
CsScript cs.AddCode("")
IDispatch dp=cs.CreateObject("Test")
BSTR _s1=s1
BSTR _s2=s2
BSTR _s3=s3
dp.StringTest(&_s1 &_s2 &_s3)
err
,out _error.description
s1=_s1
s2=_s2
s3=_s3
out "s1 = %s, s2 = %s, s3 = %s" s1 s2 s3

#ret
//C# code
using System;

public class Test
{
,public static void StringTest(ref string x, ref string y, ref string z)
,{
,,x = "x_string";
,,y = "y_string";
,,z = "z_string";
,}
}
#8
The C# method must be not static.
#9
Arrg bummer!
Thanks for the tip! Big Grin


Forum Jump:


Users browsing this thread: 3 Guest(s)