Boolean int conversion issue

后端 未结 2 1454
庸人自扰
庸人自扰 2020-12-05 22:52

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

void reqMktDataEx(int tickerId, IContract contract, string general         


        
相关标签:
2条回答
  • 2020-12-05 23:25

    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.

    0 讨论(0)
  • 2020-12-05 23:30

    One more way is to have extension method:

    public static class BooleanExtensions
    {
        public static int ToInt(this bool value)
        {
            return value ? 1 : 0;
        }
    }
    

    then it can be used:

    bool result = false;
    result.ToInt();
    
    0 讨论(0)
提交回复
热议问题