C#boolean int转换问题

我正在开发一个交易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作为bool处理错误,根据此Web 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")) 

投诉是输入字符串格式不正确。 确保方法参数的格式正确。

MY GUESS:这是C#的内部配置,它不将0视为false,将1视为true。 有什么办法可以解决吗?

首先编辑

正如下面一位专业程序员所怀疑的那样,我在这里为他发布合同类和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"; } } } The method I use to call the reqMktDataEx: implementation first, simple inheritance: 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); } **Second Edit**: System.InvalidCastException was unhandled Message=Unable to cast object of type 'InteractiveBrokersTradingSystem.Forex' to type 'TWSLib.IContract'. Source=InteractiveBrokersTradingSystem It seems that here is some casting problem between the forex class I defined and the Icontract com thing. Here is my new definition: 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类inheritance自TWS.IContract。 怎么不能连续投射到Icontract?

没有将bool隐式转换为int 。 只有一个明确的:

 Convert.ToInt32(someBool) // or... someBool ? 1 : 0 

从您链接的那个网站 :

首先,您不能隐式地将bool转换为int。 C#编译器使用此规则来强制执行程序正确性。 规则是要求您不能在if语句中测试整数。

编辑

int没有无穷大的概念。 只有floatdouble 。 这意味着它与该参数无关,除非该参数仅控制实际崩溃的代码流。 这仍然意味着它不是导致问题的转换。

你得到int.Parse("false")的不同错误,因为它期望一个数字,而不是一个真/假值。 这将始终在运行时抛出exception,但它会引入代码,而不是库的代码。

我开始认为这是你提供AUDUSD的第二个参数, contract

还有一种方法是使用扩展方法:

 public static class BooleanExtensions { public static int ToInt(this bool value) { return value ? 1 : 0; } } 

然后它可以使用:

 bool result = false; result.ToInt();