sza
2010-05-26 05:48:56
+1
A:
as
checks if it can cast it, if it can't, it sets lblSome to null. Normal casting, (Label)
, doesn't do that check for you, just gives you an InvalidCastException
instead. However, as
doesn't work with non-nullable structs.
Joel
2010-05-26 05:51:14
"as only works with nullable objects, so you can't use as with structs.": Nullables *are* structs. I think you mean you can't use it with non-nullable structs. http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
Mark Byers
2010-05-26 05:58:31
Thanks for that. Fixed.
Joel
2010-05-26 16:46:12
A:
if e.Item.FindControl("lblMyLable")
returns an object that is not Label (Label)e.Item.FindControl("lblMyLable")
will result in InvalidCastException
.
while e.Item.FindControl("lblMyLable") as Label
will result in null being assigned to lblSome.
Itay
2010-05-26 05:51:16