Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

c# - Boolean int conversion issue

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)

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".

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) 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".

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. 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.

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?

First Edit

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

From that site you linked:

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...