I am not sure if this is the right group but I didn't see languages group.
I have the following statement:
if (e.SourceRow["COL1"] as string != e.TargetRow["COL2"] as string)
{
// do something
}
The COL1 and COL2 are both double type. e.SourceRow is System.Data.DataRow
and the indexer will return an object. When I execute this, it will never
execute inside the braces.
The values of the left and the right are definitely different. And when
I print out the value of "e.SourceRow["COL1"] as string", it correctly prints
out the value. Same for COL2. However, the boolean expression returns false,
meaning both values are equal. How can this be?
I suspect this has something to do with the use of as operator as it is not
meant to be used for value types such as double. But the operands correctly
convert from object to string and they do print different string values for
each. Can someone elaborate what is going on? Nevermind that I shouldn't
be comparing doubles as strings or anything else. I am just curious why
this works as it is.
Thanks in advance.
Jiho Han
Senior Software Engineer
Infinity Info Systems
The Sales Technology Experts
Tel: 212.563.4400 x216
Fax: 212.760.0540
jhan@infinityinfo.com
www.infinityinfo.com
> I am not sure if this is the right group but I didn't see languages group.
See microsoft.public.dotnet.framework.languages.csharp
> I have the following statement:
>
[quoted text clipped - 17 lines]
> be comparing doubles as strings or anything else. I am just curious why
> this works as it is.
If e.SourceRow["COL1"] and e.SourceRow["COL2"] both evaluate to
doubles, then each of them "as string" will be null - so they'll be
equal. If you want to *convert* them to strings, call ToString instead:
if (e.SourceRow["COL1"].ToString() != e.SourceRow["COL2"].ToString())
{
...
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jiho Han - 30 Mar 2006 19:02 GMT
Thanks Jon.
That's what I thought. I knew there was no way for an as operator to work
on a double.
When I printed out the values, I must have done it like this:
Debug.WriteLine("test = " + e.SourceRow["COL1"] as string);
it was the operator precedence rule that fooled me!
Thanks for the pointer to the language group as well.
>> I am not sure if this is the right group but I didn't see languages
>> group.
[quoted text clipped - 35 lines]
> ...
> }