无法将要分配给的 class 实例声明为库函数的输出参数
Cannot declare a class instance to be assigned to as an out parameter of library function
我的 VB.NET 脚本需要使用用 C# 编写的库,特别是 Connect()
方法,它有一个 out
参数,即 class。
VB.NET 调用库方法的代码如下所示:
Dim devInfo As DeviceInfoType
myDevice.Connect(ChannelType.DEV_CHANNEL, 0, devInfo)
声明在 C# 库中的样子:
// `ChannelType` is just an enum.
void Connect(ChannelType channel, UInt16 connectMode, out DeviceInfoType devInfo);
public class DeviceInfoType
{
public GetIdType GetId { get; set; }
}
当我调用 myDevice.Connect()
时,计算机等待几秒钟然后抛出一个错误(所以我假设设备正在连接,并且一旦建立连接,该方法就会尝试将设备信息分配给out
参数):
Object not set to instance of object
我可以调用 myDevice
的其他方法就好了,所以我认为这与 DeviceInfoType
参数有关。
C# 库的作者告诉我,C# 用户必须像这样调用 Connect()
,但是 VB.NET:
中当然没有 out
关键字
myDevice.Connect(ChannelType.DEV_CHANNEL, 0x00, out devInfo);
我也试过像这样声明devInfo
,但仍然得到同样的错误:
Dim devInfo As DeviceInfoType = Nothing
Dim devInfo As New DeviceInfoType
原来是库函数在做某事导致了 Object not set to instance of object
异常。发生这种情况是因为 0x00
参数不在有效范围内;一旦我传递了有效范围内的值,就没有问题了。
我的 VB.NET 脚本需要使用用 C# 编写的库,特别是 Connect()
方法,它有一个 out
参数,即 class。
VB.NET 调用库方法的代码如下所示:
Dim devInfo As DeviceInfoType
myDevice.Connect(ChannelType.DEV_CHANNEL, 0, devInfo)
声明在 C# 库中的样子:
// `ChannelType` is just an enum.
void Connect(ChannelType channel, UInt16 connectMode, out DeviceInfoType devInfo);
public class DeviceInfoType
{
public GetIdType GetId { get; set; }
}
当我调用 myDevice.Connect()
时,计算机等待几秒钟然后抛出一个错误(所以我假设设备正在连接,并且一旦建立连接,该方法就会尝试将设备信息分配给out
参数):
Object not set to instance of object
我可以调用 myDevice
的其他方法就好了,所以我认为这与 DeviceInfoType
参数有关。
C# 库的作者告诉我,C# 用户必须像这样调用 Connect()
,但是 VB.NET:
out
关键字
myDevice.Connect(ChannelType.DEV_CHANNEL, 0x00, out devInfo);
我也试过像这样声明devInfo
,但仍然得到同样的错误:
Dim devInfo As DeviceInfoType = Nothing
Dim devInfo As New DeviceInfoType
原来是库函数在做某事导致了 Object not set to instance of object
异常。发生这种情况是因为 0x00
参数不在有效范围内;一旦我传递了有效范围内的值,就没有问题了。