c# code...
在c#中定义Return、Arithmetic两个类,并在Arithmetic的computing方法中out Return类的实例
using System;
using System.Runtime.InteropServices;
namespace MultiReturn_Class
{
public class Return
{
public double a;
public double b;
public string c;
}
[ComVisible(true)]
public class Arithmetic
{
[ComVisible(true)]
public void computing(double a, double b, out double c, out double d,out Return back)
{
c = a + b;
d = a - b;
back = new Return();
back.a = c;
back.b = d;
back.c = "四则运算";
}
}
}
VBS code
dim myObj,back
Set myObj = CreateObject("MultiReturn_Class.Arithmetic")
call myObj.computing(3,1,c,d,back)
msgbox(back.a)
msgbox(back.b)
msgbox(back.c)
Error....800A0005
无效的过程调用或参数:'computing'
Most often reason:在过程调用中传递了一个无效参数。这可能是由于参数超出范围,或包含无效数据。另外,有可能在不适当的时间对过程进行了调用。
考虑到VBS的部分变量类型异于C#(参考我的第一篇博文),对out Return back进行修改,改成out Object back
C#中,Object可以看做是所有类型的基类。。因此在这里需要对Arithmetic.computing代码进行修改,new一个Return的实例,并将其赋给Object back
public void computing(double a, double b, out double c, out double d,out Object back)
{
c = a + b;
d = a - b;
Return reverse = new Return();
reverse.a = c;
reverse.b = d;
reverse.c = "四则运算";
back = reverse;
}
运行VBS...
继续报错
错误出现在msgbox(back.a)所在的第四行。。证明vbs实现了调用computing方法,但是对Object back的赋失败,考虑在computing中子类对基类赋值过程中new的Return实例,尝试在程序中使class Return对com可见。。
[ComVisible(true)]
public class Return
{
public double a;
public double b;
public string c;
}
再次运行VBscript得正解