Posts: 133
Threads: 15
Joined: Jun 2014
Hi Gintaras,
Does QM supports passing by reference using C#?
Example:
Macro PassByRef
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]
Posts: 12,071
Threads: 140
Joined: Dec 2002
CsFunc doesn't.
Create object and call functions through IDispatch.
Macro Macro2527
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;
,}
}
Posts: 133
Threads: 15
Joined: Jun 2014
It works great.
Thank You!
Posts: 133
Threads: 15
Joined: Jun 2014
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
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";
,}
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
Need BSTR.
Macro Macro2527
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";
,}
}
Posts: 133
Threads: 15
Joined: Jun 2014
Thanks for quick solution.
It works like a charm!
Posts: 133
Threads: 15
Joined: Jun 2014
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
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";
,}
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
The C# method must be not static.
Posts: 133
Threads: 15
Joined: Jun 2014
Arrg bummer!
Thanks for the tip!
|