Thursday, March 01, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

Testing for types using "is" and "as" operators

Aug 21

Written by:
Thursday, August 21, 2008 11:40 PM  RssIcon

Ever find yourself trying to figure out what sender is in an event handler?  Have you resorted to using String comparisons thinking there has to be a better way?  Well, there is!

We all know in standard event handlers you get two parameters, the first of type object and the second is a type of EventArgs.  But how do you know what kind of object/datatype the sender is?  Do you know if it's a Button, PictureBox, TextBox or even one of your own objects?


You could use the old C-style casts if you're 100% sure what type of object you're dealing with. 

BUUUUUUT what happens on the off chance your buddy in the next cubicle starts using your event handler from a different source, like an image for example?  Hhhhmmm can you say unhandled exception?


Another way is to do string comparisons.  But even with the umpteen different ways to comare strings (two are illustrated), it still seems very cumbersome and time consuming. 

So what's a coder supposed to do?  No, bearing your head in the sand is only for Sens  fans. :> DOH!  Sorry, showing a bit of my Habs GO HABS GO!!!! loyalty there. :>  But seriously, .NET has two operators you can use just for this purpose, the "is" and the "as" operators.

 The (overly simplified :>) code below illustrates using both the "is" and "as" operators.

Is Operator

Makes your code clean to read and maintain and is great for when you're not necessarily going to do anything with that datatype you're checking against.  In my example I am, but in general, if you were just checking for a Button and then would do something else, then the "is" operator is your man, uh, keyword. :>

As Operator

However, if what you do need to do is cast the object and get properties, call methods, etc, then maybe the As operator is your keyword of choice.  This operator has one caveat, you'll have to check for null before using it!  Otherwise you'll have a null reference exception on your hands.


The "is" operator is great for quickly checking types but only checking types, you still have to cast after that if you want to do specific types of things on that object.

The "as" operator will cast the object to the datatype you want, but you will have to check for null before using it.


References: Jay Miller: .NET Tip: Type Casting and Comparison Using "as" and "is"

Location: Blogs Parent Separator TechTidBits

2 comment(s) so far...

Re: Testing for types using "is" and "as" operators

Oh, and another thing. Why in the name of God are you posting stuff at 4:40 am. I just KNOW you're not getting up that early. Go to bed will ya??

By Darklight on   Sunday, August 24, 2008 12:33 PM

Re: Testing for types using "is" and "as" operators

HAHA The 4:40AM thing is a bug with the system. The system is using UTC time to track everything and unfortunately there's a bug with the Calendar....long story short, I was/am still playing with the dates to get it so when you click on the Calendar for a specific date, the blogs for that date come up. Cause of the bug, it's aways a day behind. :< This was written around 11:40PM at night. Late but certainly not 4:40AM! :>

By phenry on   Sunday, August 24, 2008 12:37 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement