C#布尔INT转换的问题 [英] C# boolean int conversion issue

查看:143
本文介绍了C#布尔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快照,这显然需要一个int输入这实际上表明交易者是否想快照的市场数据或没有。所以我想,如果我将它设置为非零值,那么隐含的转换将其转换为非零为布尔值真。

不过,我使用C#连接到这个API。一切都很正常,直到这一个。我想这样的:

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

一个。 无效reqMktDataEx(1澳元兑美元,100,0)
请忽略前三个参数1,澳元兑美元,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为布尔型true和0作为BOOL FALSE根据本的隐式
网络<一个href=\"http://www.dotnetperls.com/convert-bool-int\">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

乙。我想这
无效reqMktDataEx(1澳元兑美元,100,Convert.ToInt16(假))我再次得到了类似的错误。

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

℃。我再次尝试这一个:

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作为假,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";

        }
    }
}


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;


        }
    }
 }

正如你可以看到,外汇类从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?

推荐答案

有一个布尔到的隐式转换 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 不具有无穷大的概念。只有浮动双击做的。这意味着将不会与该参数,但该参数只是控制code,实际上是崩溃的流动。这仍然意味着它并不是引起问题的转换。

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不同的错误(假),因为它是期待一个数字,而不是真/假值。这将始终抛出运行时异常,但它会在抛出的的code,而不是在图书馆的code。

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.

我开始认为这是第二个参数,合约,你已经提供了哪些澳元兑美元

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

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

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