【发布时间】:2012-01-17 10:42:54
【问题描述】:
我正在开发一个交易 API(来自交互式经纪人的 activex),它有一个名为:
void reqMktDataEx(int tickerId, IContract contract, string generalDetails, int snapshot)
问题在于最后一个参数“int snapshot”,它显然需要一个 int 输入,实际上表明交易者是否想要快照市场数据。所以我猜如果我将它设置为非零,那么隐式转换会将这个非零转换为bool值“true”。
但是,我使用 c# 连接到这个 api。在这之前一切都很好。我试过这个:
A. void reqMktDataEx(1, AUDUSD, "100", 0)
请忽略前三个参数“1,AUDUSD,”100“”,唯一的问题是最后一个0作为int。我在调试过程中暂停,信息是:
“指定的转换无效。Invalidcastexception 未处理”和“从数字转换时,该数字不能是无穷大”。
在这之后,我了解到 C# 很难将 1 视为 bool true 并将 0 视为 bool false 隐含地根据此 网络http://www.dotnetperls.com/convert-bool-int
B.我试过这个
void reqMktDataEx(1, AUDUSD, "100", Convert.ToInt16(false))我又遇到了类似的错误。
C.我又试了一次:
void reqMktDataEx(1, AUDUSD, "100", int.Parse("false"))
投诉是输入字符串的格式不正确。确保您的方法参数格式正确。
我的猜测: 这是 C# 的内部配置,它不将 0 视为假,将 1 视为真。有什么办法解决吗?
首次编辑
正如下面一位专业程序员所怀疑的那样,我在这里为他发布了合同类和audusd定义。提前谢谢
namespace InteractiveBrokersTradingSystem
{
class Contract:TWSLib.IContract
{
public int conId { get; set; }
public string symbol { get; set; }
public string secType { get; set; }
public string expiry { get; set; }
public double strike { get; set; }
public string right { get; set; }
public string multiplier { get; set; }
public string exchange { get; set; }
public string primaryExchange { get; set; }
public string currency { get; set; }
public string localSymbol { get; set; }
public int includeExpired { get; set; }
public object comboLegs { get; set; }
public object underComp { get; set; }
public string comboLegsDescrip { get; set; }
public string secIdType { get; set; }
public string secId { get; set; }
}
}
namespace InteractiveBrokersTradingSystem
{
class Forex:Contract
{
public Forex(string preCurrency,string baseCurrency)
{
//conId = 14433401;
symbol = preCurrency;
secType = "CASH";
exchange = "IDEALPRO";
currency = baseCurrency;
strike = 0;
includeExpired = 0;
primaryExchange = "IDEALPRO";
}
}
}
我用来调用 reqMktDataEx 的方法: 先实现,简单继承:
public void MyReqMarketData(int tickId, IContract contract, string tickTypes, int snapshot)
{
reqMktDataEx(tickId, contract, tickTypes, snapshot);
}
private void AudButtonItemItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Forex audusd = new Forex("AUD", "USD");
_myTwsClass.MyReqMarketData(1,audusd, "100", 0);
}
二次编辑:
System.InvalidCastException was unhandled
Message=Unable to cast object of type 'InteractiveBrokersTradingSystem.Forex' to type 'TWSLib.IContract'.
Source=InteractiveBrokersTradingSystem
这似乎是我定义的外汇类和 Icontract com 之间的一些转换问题。这是我的新定义:
namespace InteractiveBrokersTradingSystem
{
class Forex
{
public int conId { get; set; }
public string symbol { get; set; }
public string secType { get; set; }
public string expiry { get; set; }
public double strike { get; set; }
public string right { get; set; }
public string multiplier { get; set; }
public string exchange { get; set; }
public string primaryExchange { get; set; }
public string currency { get; set; }
public string localSymbol { get; set; }
public int includeExpired { get; set; }
public object comboLegs { get; set; }
public object underComp { get; set; }
public string comboLegsDescrip { get;set; }
public string secIdType { get; set; }
public string secId { get; set; }
public Forex(string preCurrency,string baseCurrency)
{
//conId = 0;
//symbol = preCurrency;
//secType = "CASH";
//expiry = null;
//strike = double.Parse("0");
//right = null;
//multiplier = null;
//exchange = "IDEALPRO";
//primaryExchange = "IDEALPRO";
//currency = baseCurrency;
//localSymbol = null;
//includeExpired = 0;
//comboLegs = null;
//underComp = null;
//comboLegsDescrip = null;
//secType = null;
//secId = null;
}
}
}
如您所见,Forex 类继承自 TWS.IContract。怎么不能连续转换成Icontract?
【问题讨论】:
-
你为什么不直接叫它
reqMktDataEx(1, AUDUSD, "100", false)? -
我怀疑这与布尔值无关。
-
@Yahia:因为没有隐式转换?
-
那么问题是你为什么不直接称它为
reqMktDataEx(1, AUDUSD, "100", 0)或reqMktDataEx(1, AUDUSD, "100", 1)?