Boolean int 转换问题

问题描述 投票:0回答:5

我正在开发一个交易 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 视为 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";       
        }
    }
}

我用来调用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?

c# integer boolean
5个回答
147
投票

不存在

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

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

从您链接的网站

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

编辑

int
没有无穷大的概念。只有
float
double
可以。这意味着它不会与该参数相关,除非该参数仅控制实际崩溃的代码流。这仍然意味着问题不是由转换引起的。

您会收到

int.Parse("false")
的不同错误,因为它需要一个数字,而不是真/假值。这总是会在运行时抛出异常,但它会抛出 your 代码,而不是库的代码。

我开始认为这是第二个参数,

contract
,您已为其提供了
AUDUSD


11
投票

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

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

然后就可以使用了:

bool result = false;
result.ToInt();

0
投票

在数据库中创建字段tinyint tinyint 字段名; c# 代码

convert.toint32(fieldname.tostring());//returns 1 or 0
to get boolean value

covert.tobool(fieldname.tostring()) ;

0
投票

没有从 bool 到 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 是通用的,因为我需要在几个地方进行具体计数,包括最后一个函数,使其易于使用。此外,为了维护,将嵌入函数放在布尔值下可以使后续开发人员更有可能看到发生了什么。


0
投票

这里没有提到,因为它是一个模因答案,但是 ->

false.CompareTo(value)

© www.soinside.com 2019 - 2024. All rights reserved.