Posts: 1,102
Threads: 260
Joined: Jul 2022
Yesterday, 11:11 AM
(This post was last modified: Yesterday, 11:13 AM by Davider.)
Is there a simpler way to write an array of variables of the following variant types?
The following code works, but I think it's neither elegant nor concise.
Thanks in advance for any suggestions or help.
Function
arrVar
function` [`_A1] [`_A2] [`_A3] [`_A4] [`_A5] [`_A6]
ARRAY(VARIANT) args.create
sel getopt(nargs)
,case 1 args[]=_A1
,case 2 args[]=_A1; args[]=_A2
,case 3 args[]=_A1; args[]=_A2; args[]=_A3
,case 4 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4
,case 5 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4; args[]=_A5
,case 6 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4; args[]=_A5; args[]=_A6
Posts: 12,165
Threads: 143
Joined: Dec 2002
How the array will be used? Should it be standard QM array, or need SAFEARRAY like in CsFunc? Or maybe just VARIANT* (then it's &_A1).
Posts: 1,102
Threads: 260
Joined: Jul 2022
Yesterday, 12:24 PM
(This post was last modified: Yesterday, 01:07 PM by Davider.)
Used in scenarios like the one below
Function
arrVar
function` [`_A1] [`_A2] [`_A3] [`_A4] [`_A5] [`_A6]
CsScript x.AddCode("" 0x10000)
ARRAY(VARIANT) args.create
sel getopt(nargs)
,case 1 args[]=_A1
,case 2 args[]=_A1; args[]=_A2
,case 3 args[]=_A1; args[]=_A2; args[]=_A3
,case 4 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4
,case 5 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4; args[]=_A5
,case 6 args[]=_A1; args[]=_A2; args[]=_A3; args[]=_A4; args[]=_A5; args[]=_A6
str command=
;cd
ret x.Call("test.Call2" command args)
#ret
public class test
{
,public static string Call2(string command, params object[] args)
,{
,,Console.WriteLine(command);
,,//
,}
}
Quote:SAFEARRAY like in CsFunc
I tried, but had no luck.
Quote: VARIANT* (then it's &_A1)
That should be the idea, but I don't know how to modify the code.
The following code throws an "invalid type cast" error at runtime.
Posts: 12,165
Threads: 143
Joined: Dec 2002
Function
VariantArgsToArray
;/
function ARRAY(VARIANT)&a VARIANT&firstArg
int na = getopt(nargs 1)
a.create(na)
na*sizeof(VARIANT)
memcpy &a[0] &firstArg na
memset &firstArg 0 na
Function
arrVar
function` [`_A1] [`_A2] [`_A3] [`_A4] [`_A5] [`_A6]
CsScript x.AddCode("")
ARRAY(VARIANT) a; VariantArgsToArray a _A1
str s=
;cd
ret x.Call("test.Call2" s a)
#ret
using System;
public class test
{
,public static string Call2(string command, params object[] args)
,{
,,Console.WriteLine(command + ": " + string.Join(", ", args));
,,return null;
,}
}
Macro
Macro3503
Posts: 1,102
Threads: 260
Joined: Jul 2022
Yesterday, 01:47 PM
(This post was last modified: Yesterday, 01:51 PM by Davider.)
Thank you so much!
The code is much more elegant now and supports more parameters.
is there a way to make the function return a variant type?
public static string Call2
Posts: 12,165
Threads: 143
Joined: Dec 2002
public static object Call2
Posts: 1,102
Threads: 260
Joined: Jul 2022
Thanks again!
I can finally enjoy seamless interaction between QM and C# code.