Cerebrus:

As Joe and the article he posted state, if the as keyword fails to
cast the item it returns an exception hidden behind a null return
value.  I prefer to catch the InvalidCastException and handle it
accordingly.  Ultimately, the way this is implemented is up to the
programmer and the circumstance.


On May 20, 11: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- Hide quoted text -
>
> - Show quoted text -

Reply via email to