Reputation: 11
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
Reputation: 826
You may also want to try to use Equals:
Application["Theme"].ToString().Equals("Night")
Upvotes: 0
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