Possible Unintended Reference Comparison

Possible unintended reference comparison

The warning is because the compile-time type of lblStatus.Content is object. Therefore operator overloading chooses the ==(object, object) overload which is just a reference identity comparison. This has nothing to do with what the execution-time type of the value is.

The first or second of your options should have fixed the warning though:

if (lblStatus.Content.ToString() == "ACTIVE")
if ((string)lblStatus.Content == "ACTIVE")

Note that the first of these will throw an exception if lblStatus.Content is null. I would prefer the second form.

If you think you're still seeing a warning at that point, I suspect you either haven't rebuilt - or something is still "dirty" in your build. A full rebuild absolutely should remove the warning.

Warning 1 Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

The Possible unintended reference comparison is this one:

answer[1].Tag == "blabla2"

The Tag is an object, you are allowed to compare it to "blabla2" because string is a reference type. The compiler is "casting" your string to object and is performing a reference comparison (which will evaluate to false since they are not the same object).

To fix this, you have to cast Tag before checking. Together with the fix I mentioned above, your code would become

if (answer[0].Tag.ToString() == "bla bla" && answer[1].Tag.ToString() == "blabla2")
{
MessageBox.Show("They Match");
}
else
MessageBox.Show("They don't");

C#-Warning 2 Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

I suggest to change the if-statement to this:

if (Session["UserName"] != null && 
(Session["LoginType"] as string == "Admin" ||
Session["LoginType"] as string == "Employee")
)

Session[string key] returns an object. And if you compare an object to something using ==, you do a reference comparison. And the string literal ("Admin" for example) will never have the same reference like that object, even if this object is a string.

By casting the object into a string, the compiler knows that it has to call the equality methods of string, which compare the string's contents instead of their references.

Of course you can do a direct cast ((string)Session["LoginType"]) or call ToString(), too. But the first will throw an exception if (for some strange reason) the return object is not a string. The second will throw a NullReferenceException if (for some strange reason) the value is still null.

Possible unintended reference comparison worked as intended

The string literal "foo" is interned. It means that every time it's used, the same object is referenced.

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

That's why object.ReferenceEquals("foo", "foo") is true.

If you do the same with a dynamically created string, it will not be interned and the references will not be equal

object str = new StringBuilder().Append("f").Append("oo").ToString(); // str = "foo"
object.ReferenceEquals(str, "foo"); // false

String interning can work differently depending on implementation, that's why you can get different behavior on different environments when comparing strings by reference.

possible unintended reference comparison to get a value comparison cast the right hand side to string

Try this

if (UserText.Text.ToString() == dt.Rows[i]["CustID"].ToString() &&  
PassText.Text.ToString() == dt.Rows[i]["CustPassword"].ToString())

or this

if ((string)UserText.Text == (string)dt.Rows[i]["CustID"] && 
(string)PassText.Text == (string)dt.Rows[i]["CustPassword"])

and Re-Build your project. The error is because the compile-time type of UserText.Text is object and it fails.

Possible unintended reference comparison in combobox

SelectedItem - For gets this will return the actual object in the DataSource that is being displayed in the ComboBox. For sets if the value exists in the DataSource, it will be selected, otherwise the operation will complete without an exception but won't actually do anything.

Try to parse the object returned by SelectedItem to string using ToString() method before comparing it with other strings ("metr" & "kilometr") in condition :

if (delka_comboBox1.SelectedItem.ToString() == "metr" && delka_comboBox2.SelectedItem.ToString() == "kilometr")
{
delka_vysledek_label.Text = Convert.ToString(delka / 1000);
}

Like @Patrick mentioned in comment : SelectedItem can be null, so if you want to cover also this case you can use another cast method (string) e.g :

if ((string)delka_comboBox1.SelectedItem == "metr" && (string)delka_comboBox2.SelectedItem == "kilometr")
{
delka_vysledek_label.Text = Convert.ToString(delka / 1000);
}


Related Topics



Leave a reply



Submit