布尔 int 转换问题 [英] Boolean int conversion issue

查看:27
本文介绍了布尔 int 转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个交易 API(来自交互式经纪人的 activex),它有一个名为:

I am working on a trading API (activex from interactive brokers)which has a method called:

void reqMktDataEx(int tickerId, IContract contract, string generalDetails, int snapshot)

问题在于最后一个参数int snapshot",它显然需要一个 int 输入,实际上表明交易者是否想要快照市场数据.所以我猜如果我将它设置为非零,那么隐式转换会将这个非零转换为 bool 值true".

The issue is around the last parameter "int snapshot" which obviously requires an int input which actually indicates that whether trader wanna snapshot market data or not. So I guess that if I set it to non-zero, then the implicit conversion would convert this non-zero to be bool value "true".

但是,我使用 c# 连接到这个 api.在这之前一切都很好.我试过这个:

However, I am using c# to connect to this api. Everything was fine until this one. I tried this:

A.void reqMktDataEx(1, AUDUSD, "100", 0)请忽略前三个参数1,AUDUSD,"100",唯一的问题是最后一个0作为int.我在调试过程中暂停,信息是:指定的转换无效.Invalidcastexception 未处理"和从数字转换时,该数字不能是无穷大".

A. void reqMktDataEx(1, AUDUSD, "100", 0) Please ignore the first three parameters "1, AUDUSD, "100"", the only matter is the last one 0 as int. I got paused during debugging and the information is : "Specified cast is not valid. Invalidcastexception is unhandled" and "when casting from a number, the number must not be infinity".

在这之后,我了解到 C# 很难将 1 视为 bool true 并将 0 视为 bool false 隐含地根据此网页 http://www.dotnetperls.com/convert-bool-int

After this I learned that here is a difficulty for c# to treat 1 as bool true and 0 as bool false IMPLICITLY according this web http://www.dotnetperls.com/convert-bool-int

B.我试过这个void reqMktDataEx(1, AUDUSD, "100", Convert.ToInt16(false)) 我又遇到了类似的错误.

B. I tried this void reqMktDataEx(1, AUDUSD, "100", Convert.ToInt16(false)) I got similar error again.

C.我又试了一次:

void reqMktDataEx(1, AUDUSD, "100", int.Parse("false"))

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

the complaint is input string was not in a correct format. Make sure that you method arguments are in the right format.

我的猜测:这是 C# 的内部配置,它不将 0 视为 false 并将 1 视为 true.有什么办法解决吗?

MY GUESS: Here is a inside configuration of C# which does not treat 0 as false and 1 as true. Is there any way to solve?

首次编辑

正如下面一位专业程序员所怀疑的那样,我在这里为他发布了合同类和audusd定义.提前致谢

As suspected by one professional programmer below, I post the contract class and audusd definition here for him. thanks in advance

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 的方法:先实现,简单继承:

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);
  }

二次编辑:

  System.InvalidCastException was unhandled
  Message=Unable to cast object of type 'InteractiveBrokersTradingSystem.Forex' to type 'TWSLib.IContract'.
  Source=InteractiveBrokersTradingSystem

这似乎是我定义的外汇类和 Icontract com 之间的一些转换问题.这是我的新定义:

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 类继承自 TWS.IContract.怎么不能连续转换成Icontract?

As you can see that the Forex class inherits from the TWS.IContract. how it could not be cast to Icontract successively?

推荐答案

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

There is no implicit conversion of a bool to an int. Only an explicit one:

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

从您链接的那个网站:

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

First, you cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. It is the same rule that mandates you cannot test an integer in an if statement.

编辑

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

Edit

int doesn't have a concept of infinity. Only float and double do. This means it won't be related to that parameter, unless that parameter just controls the flow of the code that is actually crashing. Which still means it isn't the conversion causing the problem.

int.Parse("false") 出现不同的错误,因为它需要一个数字,而不是真/假值.这将始终在运行时引发异常,但它会在您的代码中引发异常,而不是在库的代码中.

You're getting a different error for int.Parse("false") because it is expecting a number, not a true/false value. This will always throw an exception at runtime, but it will throw in your code, not in the library's code.

我开始认为它是第二个参数 contract,您已为其提供了 AUDUSD.

I'm starting to think it is the second parameter, contract, for which you've supplied AUDUSD.

这篇关于布尔 int 转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆