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

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

问题描述

我正在开发一种交易API(交互式经纪人提供的activex),该API的方法为:

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.我在调试过程中暂停了,信息是: 指定的转换无效.未处理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视为布尔值true和0视为布尔值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.我再次尝试了这个:

C. I tried again this one:

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?

首次编辑

正如下面的一位专业程序员所怀疑的那样,我在这里为他张贴了合同类别和正确的定义.预先感谢

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?

推荐答案

没有boolint的隐式转换.只是一个明确的:

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"),您会收到另一个错误,因为它期望的是数字,而不是true/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.

我开始认为这是您为其提供了AUDUSD的第二个参数contract.

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

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

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