Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An array of variant types
#1
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
Code:
Copy      Help
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
#2
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).
#3
Used in scenarios like the one below

Function arrVar
Code:
Copy      Help
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.
Code:
Copy      Help
VARIANT* args=&_A1
#4
Function VariantArgsToArray
Code:
Copy      Help
;/
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
Code:
Copy      Help
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
Code:
Copy      Help
arrVar("text", 1000)
#5
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
#6
public static object Call2
#7
Thanks again!  Heart

I can finally enjoy seamless interaction between QM and C# code.  Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)