I read an interesting opinion on "as", and kind of agree with it: http://www.dotnetjunkies.com/WebLog/johnwood/archive/2006/03/09/135830.aspx
Basically, he's saying that if you know what it's supposed to be cast to, then why not a direct cast? If it's not the type you were expecting, then you probably would want to throw an InvalidCastException, since obviously something went wrong. And if you don't want to throw that exception, using the "is" statement in advance works equally as well so you wouldn't even try the cast. I guess it's a matter of opinion, and your particular circumstance. If you're in a situation where you want to attempt a cast and move forward upon failure, then maybe "as" works better for you. On May 20, 10:30 pm, Cerebrus <[email protected]> wrote: > The_Fruitman: > > The way I prefer is using the "as" keyword. It obviates the need to > handle the InvalidCastException thrown in explicit casts: > > TextBox txt1 = e.Item.FindControl("NameOfTextBox") as TextBox; > if (txt1 != null) > { > // Successful cast. > > } > > I perform almost all casts using the as keyword. > > On May 19, 6:58 pm, The_Fruitman <[email protected]> wrote: > > > The way I've done it in the past is > > TextBox txt1 = (TextBox)(e.Item.FindControl("NameOfTextBox")); > > > Where e is type DataGridCommandEventArgs
