当没有可用的构造函数时,Visual Basic Dim 在 C# 中等效于使用接口
Visual Basic Dim As equivalent in C# using an Interface when there is no available constructor
各位:
抱歉,这是一个冗长的问题。 TL;DR:如果 ISomeAPIInterface 没有默认构造函数,我如何在 C# 中执行以下 Visual Basic 代码:
Visual Basic: Dim foo As ISomeAPIInterface
一开始我认为这是一个简单的练习。 Interactive Brokers 有一个 ActiveX API,可以与 Visual Basic、C# 和 C++/CLI 一起使用,以处理市场数据和提交订单。总的来说有一组请求方法,一组回调方法(用于异步消息传递)和一组接口,分别对应不同的消息参数和其他请求数据和下订单所需的对象。
例如,要下订单,可以使用带有构造函数 .createOrder() 的接口 IOrder。
我已经使用 VB 和 C# 编写了数十个这样的示例,但我仍然停留在这个示例上。
对于组合订单,有一个接口没有构造方法。
IB ActiveX API 具有以下处理组合订单的接口:
IComboOrderLegList with constructor .createComboLegList()
IOrder with constructor .createOrder()
IContract with constructor .createContract()
IComboLeg with no constructor
在 Visual Basic 中(假设 AxTws1 是实现 IB API 的 ActiveX 控件)这不是问题,因为我们可以使用 Dim x As Interface,如下例所示:
' Create a list of Combo Legs
Dim cllComboOrderLegs = AxTws1.createComboLegList()
' Create Leg 1 of the order - this works fine in Visual Basic
Dim clLeg1 As TWSLib.IComboLeg
' Create Leg 2 of the order - this works fine in Visual Basic
Dim clLeg2 As TWSLib.IComboLeg
' Create the master order
Dim ordMasterOrder = AxTws1.createOrder()
' Create the master contract that will hold the legs
Dim cMasterContract = AxTws1.createContract()
' Fill in the Combo Leg information for Leg 1
clLeg1 = cllComboOrderLegs.Add()
clLeg1.conId = 13457689
clLeg1.ratio = 1
clLeg1.action = "BUY"
clLeg1.exchange = "SMART"
' Fill in the Combo Leg information for Leg 2
clLeg2 = cllComboOrderLegs.Add()
clLeg2.conId = 13457779
clLeg2.ratio = 1
clLeg2.action = "BUY"
clLeg2.exchange = "SMART"
' Fill in the master contract
cMasterContract.symbol = "MSFT"
cMasterContract.secType = "BAG"
cMasterContract.exchange = "SMART"
cMasterContract.currency = "USD"
cMasterContract.comboLegs = cllComboOrderLegs
' Fill in the Master Order
ordMasterOrder.action = "BUY"
ordMasterOrder.totalQuantity = 1
ordMasterOrder.orderType = "MKT"
' Place the order (OrderID, Contract, Order)
AxTws1.placeOrderEx(1234, cMasterContract, ordMasterOrder)
但是在 C# 中,我们尝试使用相同的 ActiveX 控件做同样的事情:
// Create a list of Combo Legs
TWSLib.IComboLegList cllComboOrderLegs = axTws1.createComboLegList();
// Create Leg 1 of the order
TWSLib.IComboLeg clLeg1 = new ???????? There is no .createComboLeg constructor
// Create Leg 2 of the order
TWSLib.IComboLeg clLeg2 = new ????????
// Create the master order
TWSLib.IOrder ordMasterOrder = axTws1.createOrder();
// Create the master contract that will hold the legs
TWSLib.IContract cMasterContract = axTws1.createContract();
// Fill in the Combo Leg information for Leg 1
clLeg1 = cllComboOrderLegs.Add();
clLeg1.conId = 3345552; // contractID
clLeg1.ratio = 1;
clLeg1.action = "BUY";
clLeg1.exchange = "SMART";
// Fill in the Combo Leg information for Leg 2
clLeg2 = cllComboOrderLegs.Add();
clLeg2.conId = 3345121; // contractID
clLeg2.ratio = 1;
clLeg2.action = "BUY";
clLeg2.exchange = "SMART";
// Fill in the master contract
cMasterContract.symbol = "MSFT";
cMasterContract.secType = "BAG";
cMasterContract.exchange = "SMART";
cMasterContract.currency = "USD";
cMasterContract.comboLegs = cllComboOrderLegs;
// Fill in the Master Order
ordMasterOrder.action = "BUY";
ordMasterOrder.totalQuantity = 1;
ordMasterOrder.orderType = "MKT";
// Place the order (OrderID, Contract, Order)
axTws1.placeOrderEx(2345, cMasterContract, ordMasterOrder);
我尝试围绕 IComboOrder 创建自己的包装器 class 但是
每当我尝试使用它时,C# 都会抱怨我的不匹配
class 和 IComboOrder。
也许我在这里遗漏了一些非常基本的东西,但 VB 似乎能够
自动做一些我不知道如何在 C# 中做的事情。换一种说法
如果我在 VB 中这样做:
' 将 clLeg1 调暗为 TWSLib.IComboLeg
C# 中的等价物是什么?
如果有帮助,我已将两个项目添加到 GitHub。 Visual Basic 风格(工作):
https://github.com/rholowczak/IB_Multileg_Orders_Winforms_VisualBasic
C# 风格(不工作):
https://github.com/rholowczak/IB_Multileg_Orders_Winforms_CSharp
建议?
是什么让您认为您需要 构造函数? VB 也从来没有打过电话。
TWSLib.IComboLeg clLeg1;
这相当于 VB 行:
Dim clLeg1 As TWSLib.IComboLeg
正是这一行,实际上用两种语言创建了一个真实的对象(加上或减去分号):
clLeg1 = cllComboOrderLegs.Add();
各位: 抱歉,这是一个冗长的问题。 TL;DR:如果 ISomeAPIInterface 没有默认构造函数,我如何在 C# 中执行以下 Visual Basic 代码:
Visual Basic: Dim foo As ISomeAPIInterface
一开始我认为这是一个简单的练习。 Interactive Brokers 有一个 ActiveX API,可以与 Visual Basic、C# 和 C++/CLI 一起使用,以处理市场数据和提交订单。总的来说有一组请求方法,一组回调方法(用于异步消息传递)和一组接口,分别对应不同的消息参数和其他请求数据和下订单所需的对象。 例如,要下订单,可以使用带有构造函数 .createOrder() 的接口 IOrder。 我已经使用 VB 和 C# 编写了数十个这样的示例,但我仍然停留在这个示例上。 对于组合订单,有一个接口没有构造方法。
IB ActiveX API 具有以下处理组合订单的接口:
IComboOrderLegList with constructor .createComboLegList()
IOrder with constructor .createOrder()
IContract with constructor .createContract()
IComboLeg with no constructor
在 Visual Basic 中(假设 AxTws1 是实现 IB API 的 ActiveX 控件)这不是问题,因为我们可以使用 Dim x As Interface,如下例所示:
' Create a list of Combo Legs
Dim cllComboOrderLegs = AxTws1.createComboLegList()
' Create Leg 1 of the order - this works fine in Visual Basic
Dim clLeg1 As TWSLib.IComboLeg
' Create Leg 2 of the order - this works fine in Visual Basic
Dim clLeg2 As TWSLib.IComboLeg
' Create the master order
Dim ordMasterOrder = AxTws1.createOrder()
' Create the master contract that will hold the legs
Dim cMasterContract = AxTws1.createContract()
' Fill in the Combo Leg information for Leg 1
clLeg1 = cllComboOrderLegs.Add()
clLeg1.conId = 13457689
clLeg1.ratio = 1
clLeg1.action = "BUY"
clLeg1.exchange = "SMART"
' Fill in the Combo Leg information for Leg 2
clLeg2 = cllComboOrderLegs.Add()
clLeg2.conId = 13457779
clLeg2.ratio = 1
clLeg2.action = "BUY"
clLeg2.exchange = "SMART"
' Fill in the master contract
cMasterContract.symbol = "MSFT"
cMasterContract.secType = "BAG"
cMasterContract.exchange = "SMART"
cMasterContract.currency = "USD"
cMasterContract.comboLegs = cllComboOrderLegs
' Fill in the Master Order
ordMasterOrder.action = "BUY"
ordMasterOrder.totalQuantity = 1
ordMasterOrder.orderType = "MKT"
' Place the order (OrderID, Contract, Order)
AxTws1.placeOrderEx(1234, cMasterContract, ordMasterOrder)
但是在 C# 中,我们尝试使用相同的 ActiveX 控件做同样的事情:
// Create a list of Combo Legs
TWSLib.IComboLegList cllComboOrderLegs = axTws1.createComboLegList();
// Create Leg 1 of the order
TWSLib.IComboLeg clLeg1 = new ???????? There is no .createComboLeg constructor
// Create Leg 2 of the order
TWSLib.IComboLeg clLeg2 = new ????????
// Create the master order
TWSLib.IOrder ordMasterOrder = axTws1.createOrder();
// Create the master contract that will hold the legs
TWSLib.IContract cMasterContract = axTws1.createContract();
// Fill in the Combo Leg information for Leg 1
clLeg1 = cllComboOrderLegs.Add();
clLeg1.conId = 3345552; // contractID
clLeg1.ratio = 1;
clLeg1.action = "BUY";
clLeg1.exchange = "SMART";
// Fill in the Combo Leg information for Leg 2
clLeg2 = cllComboOrderLegs.Add();
clLeg2.conId = 3345121; // contractID
clLeg2.ratio = 1;
clLeg2.action = "BUY";
clLeg2.exchange = "SMART";
// Fill in the master contract
cMasterContract.symbol = "MSFT";
cMasterContract.secType = "BAG";
cMasterContract.exchange = "SMART";
cMasterContract.currency = "USD";
cMasterContract.comboLegs = cllComboOrderLegs;
// Fill in the Master Order
ordMasterOrder.action = "BUY";
ordMasterOrder.totalQuantity = 1;
ordMasterOrder.orderType = "MKT";
// Place the order (OrderID, Contract, Order)
axTws1.placeOrderEx(2345, cMasterContract, ordMasterOrder);
我尝试围绕 IComboOrder 创建自己的包装器 class 但是 每当我尝试使用它时,C# 都会抱怨我的不匹配 class 和 IComboOrder。
也许我在这里遗漏了一些非常基本的东西,但 VB 似乎能够 自动做一些我不知道如何在 C# 中做的事情。换一种说法 如果我在 VB 中这样做: ' 将 clLeg1 调暗为 TWSLib.IComboLeg
C# 中的等价物是什么?
如果有帮助,我已将两个项目添加到 GitHub。 Visual Basic 风格(工作): https://github.com/rholowczak/IB_Multileg_Orders_Winforms_VisualBasic
C# 风格(不工作): https://github.com/rholowczak/IB_Multileg_Orders_Winforms_CSharp
建议?
是什么让您认为您需要 构造函数? VB 也从来没有打过电话。
TWSLib.IComboLeg clLeg1;
这相当于 VB 行:
Dim clLeg1 As TWSLib.IComboLeg
正是这一行,实际上用两种语言创建了一个真实的对象(加上或减去分号):
clLeg1 = cllComboOrderLegs.Add();