Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean int conversion issue

Tags:

c#

int

boolean

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?

like image 268
Wenhao.SHE Avatar asked Dec 10 '11 14:12

Wenhao.SHE


People also ask

Can you convert boolean to integer?

To convert boolean to integer, let us first declare a variable of boolean primitive. boolean bool = true; Now, to convert it to integer, let us now take an integer variable and return a value “1” for “true” and “0” for “false”.

Can you cast a bool to an int c#?

Use the ConvertToInt32 Statement to Convert Boolean to Integer in C# Traditionally, there is no implicit conversion of data type from boolean to an integer. However, the Convert. ToInt32() method converts a specified value to a 32-bit signed integer.

How do you convert boolean to long?

To convert an boolean value to long we simply map the boolean true value to 1 in long and false value to 0 in long. long longValue = booleanValue ? 1 : 0; The following Java example code we convert a boolean true value to long.


Video Answer


1 Answers

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.

like image 189
Merlyn Morgan-Graham Avatar answered Oct 04 '22 04:10

Merlyn Morgan-Graham