Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2006

Tip: Looking for answers? Try searching our database.

as operator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jiho Han - 30 Mar 2006 18:11 GMT
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
Jon Skeet [C# MVP] - 30 Mar 2006 18:34 GMT
> 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]
> ...
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.