Mark Williams
Mark Williams

Reputation: 612

Cannot convert type bool to int

I lost my source code and had to decompile from a DLL and am receiving the following error in the code of a LINQ to SQL DataContext class.

Error CS0030 Cannot convert type 'bool' to 'int'

It looks like VS generated code that is using int instead of bool for some strange reason. See starred line below. What should I do to fix this casting error? It's used over a hundred times in this class.

    public int? OrderBy
    {
        get
        {
            return this._OrderBy;
        }
        set
        {
            int? nullable = this._OrderBy;
            int? nullable1 = value;
            **if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault() ? 1 : (int)(nullable.HasValue != nullable1.HasValue)) != 0)**
            {
                this.SendPropertyChanging();
                this._OrderBy = value;
                this.SendPropertyChanged("OrderBy");
            }
        }
    }

EDIT: The answer to this question is "get a better decompiler." The code above was generated by Telerik Just Decompile. ILSpy generates code tthat actually makes sense. in this instance:

    [Column(Storage = "_OrderBy", DbType = "Int", UpdateCheck = UpdateCheck.Never), DataMember(Order = 6)]
    public int? OrderBy
    {
        get
        {
            return this._OrderBy;
        }
        set
        {
            if (this._OrderBy != value)
            {
                this.SendPropertyChanging();
                this._OrderBy = value;
                this.SendPropertyChanged("OrderBy");
            }
        }
    }

This question should probably just be deleted. Advise in comments.

Upvotes: 1

Views: 6162

Answers (3)

Clay Ver Valen
Clay Ver Valen

Reputation: 1073

I believe this close to what a human would have written (instead of a decompiler).

Not sure why OrderBy is a int?, but left that alone...

EDIT

Also note that the conditional operator in your post returned 1 when true and a bool when false. After fixing the issue in your post (can't convert bool to int), you'd be getting a can't convert int to bool error.

int? _OrderBy;
public int? OrderBy
{
    get
    {
        return _OrderBy;
    }
    set
    {
        if ((_OrderBy.GetValueOrDefault() != value.GetValueOrDefault()) || (_OrderBy.HasValue != value.HasValue))
        {
            SendPropertyChanging();
            _OrderBy = value;
            SendPropertyChanged("OrderBy");
        }
    }
}

Upvotes: 0

petelids
petelids

Reputation: 12815

You can't directly cast a bool to an int:

(int)(nullable.HasValue != nullable1.HasValue) //invalid

but you can use Convert.ToInt32:

Convert.ToInt32(nullable.HasValue != nullable1.HasValue) //valid

So your code should be:

if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault() ? 1 : Convert.ToInt32(nullable.HasValue != nullable1.HasValue)) != 0)

However, I think you can just do a straightforward comparison due to "lifted" operators. From the C# Spec:

For the equality operators == [and] != a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

So you could just use:

if (nullable != nullable1)

Which of course means you can elide the nullable and nullable1 variables altogether and use

if (this._OrderBy != value)

Upvotes: 1

Robert McKee
Robert McKee

Reputation: 21477

You probably want this:

if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault()) || (nullable.HasValue != nullable1.HasValue))

that should give the same result as what you had, but is a lot more readable.

Upvotes: 1

Related Questions