Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Possible unintended reference comparison
Message
From
05/03/2010 09:03:21
 
 
To
05/03/2010 08:43:19
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01452752
Message ID:
01452776
Views:
30
>>>Hi,
>>>
>>>I have this line of code:
>>>
>>>
if (dt.Rows[0]["ClientType"] == "Individual")
>>>
>>>which gives me this warning:
>>>
>>>
Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'
>>>
>>>I have tried things like this:
>>>
>>>
if ((String)(dt.Rows[0]["ClientType"]) == "Individual")
>>>
>>>and:
>>>
>>>
if ((dt.Rows[0]["ClientType"]).ToString() == "Individual")
>>>
>>>neither of which help.
>>>
>>>The code runs fine, but I would like to understand the warning and what do I need to do here?
>>
>>Shouldn't it be (string) rather than (String) in the first attempt or was that just a typo in the post ?
>
>I had actually tried both as I still don't understand the difference between the two.

Hi,
string is a Type. String is a class. But I think either should work in this case:
DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ClientType",typeof(string)));
            dt.Columns.Add(new DataColumn("Employed",typeof(bool)));

            DataRow r = dt.NewRow();
            r["ClientType"] = "StringThing";
            r["Employed"] = true;

            if (((String)r["ClientType"]) == "Fred") { }

            if (((string)r["ClientType"]) == "StringThing") { }

            if ((bool)r["Employed"]) { }
Hopefully the last line answers your other question .....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform