user3063373
user3063373

Reputation: 11

Getting the error: Possible Unintended Reference comparison; to get a value comparison, cast the left hand side to type 'string'

I'm getting the error: Possible Unintended Reference comparison; to get a value comparison, cast the left hand side to type 'string'

Here's the code generating this error

protected void Page_PreInit(object sender, EventArgs e)
{
    if (Application.["Theme"] == "Classic")
    {
        MasterPageFile = "Classic.master";
    }
    else if (Application["Theme"] == "Night")
    {
        MasterPageFile = "Night.master";
    }
}

I tried putting .ToString after ["Theme"] but then it says "Operator '==' cannot be applied to operands of type 'method group' and 'string'

Upvotes: 0

Views: 1530

Answers (2)

brclz
brclz

Reputation: 826

You may also want to try to use Equals:

Application["Theme"].ToString().Equals("Night")

Upvotes: 0

Ant P
Ant P

Reputation: 25231

That's not an error, it's a Resharper warning. It means you're comparing an object to a string, which is going to compare the references, when you actually want to compare the two strings' values. To do this, you need to cast the object to a string first.

You need to actually call the method, not compare the method to your string:

if (Application["Theme"].ToString() == "Classic")

Upvotes: 1

Related Questions