布尔整数转换问题
本文关键字:问题 转换 整数 布尔 | 更新日期: 2023-09-27 18:17:39
我正在开发一个交易API(来自盈透证券的activex(,它有一个叫做:
void reqMktDataEx(int tickerId, IContract contract, string generalDetails, int snapshot)
问题在于最后一个参数"int snapshot",它显然需要一个 int 输入,这实际上表明交易者是否想要快照市场数据。所以我想如果我将其设置为非零,那么隐式转换会将这个非零转换为bool
值"true"。
但是,我正在使用 c# 连接到此 api。一切都很好,直到这个。我试过这个:
一个。 void reqMktDataEx(1, AUDUSD, "100", 0)
请忽略前三个参数"1,AUDUSD,"100",唯一的问题是最后一个0作为int。我在调试过程中暂停了,信息是:"指定的强制转换无效。无效转换异常是未处理的"和"从数字转换时,该数字不得是无穷大"。
在此之后,我了解到 c# 很难根据这个隐式地将 1 视为布尔值真,将 0 视为布尔假网络 http://www.dotnetperls.com/convert-bool-int
二.我试过这个 void reqMktDataEx(1, AUDUSD, "100", Convert.ToInt16(false))
我再次遇到类似的错误。
三.我又试了一次:
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;
}
}
}
如您所见,外汇类继承自TWS。噗噗噗。怎么不能连续投给我?
bool
隐式转换为int
。 只有一个明确的:
Convert.ToInt32(someBool)
// or...
someBool ? 1 : 0
从您链接的该站点:
首先,您不能从 bool 隐式转换为 int。C# 编译器使用此规则强制程序正确性。这是相同的规则,要求您不能在 if 语句中测试整数。
编辑
int
没有无限的概念。 只有float
和double
这样做。 这意味着它不会与该参数相关,除非该参数仅控制实际崩溃的代码流。 这仍然意味着它不是导致问题的转换。
您收到不同的错误int.Parse("false")
因为它需要一个数字,而不是真/假值。 这将始终在运行时引发异常,但它会抛出您的代码,而不是库的代码。
我开始认为这是第二个参数,contract
,您已经为其提供了AUDUSD
.
另一种方法是使用扩展方法:
public static class BooleanExtensions
{
public static int ToInt(this bool value)
{
return value ? 1 : 0;
}
}
然后可以使用:
bool result = false;
result.ToInt();
在数据库中使字段 tinyint天音字段名;C# 代码
convert.toint32(fieldname.tostring());//returns 1 or 0
to get boolean value
covert.tobool(fieldname.tostring()) ;
没有从布尔值隐式转换为 int。所以自己做。然后将它们加起来。
Boolean
byFinancialCenter = false,
byProvider = false,
byServiceSite = false,
byProcedure = false;
int b2i(bool source) => source ? 1 : 0;
int ForeignKeyCount() => b2i(byFinancialCenter) + b2i(byProvider) + b2i(byServiceSite) + b2i(byProcedure);
b2i 是通用的,因为我需要在几个地方使用特定的计数,包括最后一个函数使其易于使用。此外,对于维护而言,将嵌入式函数放在布尔值下方,使得后续开发人员更有可能看到正在发生的事情。