//always works, returning a valid object into _page
_page = _httpContext.Handler as System.Web.UI.Page;
//Fails throwing the exception : Unable to cast object of type 'System.Web.DefaultHttpHandler' to type 'System.Web.UI.Page'
_page = (System.Web.UI.Page)_httpContext.Handler;
I would like to know why this happens?
EDIT:
//Fixes the problem
if(_httpContext.Handler is System.Web.UI.Page)
_page = (System.Web.UI.Page)_httpContext.Handler;
If i debug the 'as' keyword statement, i never get a null reference (object always assigned properly). However the () cast creates exceptions unless it has the if statment.
EDIT: After about 15 runs through the class i was able to get a null. Seems like it took more runs to find a null compared to how fast the () cast would catch an exception.
OLD: When there is a debug at the 'as' statement every time the class runs the break point hits - never null.
When tthere is a debug in the '()' statement within the if, every time the break point hits the cast works properly. Werid
The as operator can only be used on reference types, it cannot be overloaded, and it will return null if the operation fails. It will never throw an exception. Casting can be used on any compatible types, it can be overloaded, and it will throw an exception if the operation fails.
A cast communicates to the reader "I am certain that this conversion is legal and I am willing to take a runtime exception if I'm wrong"(I am willing to accept such error). The "as" operator communicates "I don't know if this conversion is legal or not;(Let's try once and see what happens how it goes").
The as operator is used to perform conversion between compatible reference types or Nullable types. This operator returns the object when they are compatible with the given type and return null if the conversion is not possible instead of raising an exception.
The is operator returns true if the given object is of the same type, whereas the as operator returns the object when they are compatible with the given type. The is operator returns false if the given object is not of the same type, whereas the as operator returns null if the conversion is not possible.
DirectCast() is more strict than the C# casting operator. It only allows you to cast when the item being cast already is the type you are casting to. It won't do any conversion. Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same.
//always works, returning a valid object into _page _page = _httpContext.Handler as System.Web.UI.Page;
This didn't technically work. If you'll notice _page
will be null
. It just didn't throw an error.
The as
operator is used to tell the application "I want you to try and convert this. It might not, and I know this, so don't throw an exception. I'll deal with it accordingly."
The ()
conversion is used to tell the application, "This object will cast to this type. If it doesn't something is wrong, and I need to know about it."
The difference between the two casts (and when you should use them) is when you "think" something is castable to another type and when you "know" something is castable to another type.
Here is an article by Eric Lippert on the subject (changed to his blog not re-feeded): http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With