The Last Gunslinger <jb******@yahoo.com> wrote:
In addition to the short form given by the others, this method is a bit
safer:
TextBox tb = controlTemplate as TextBox;
if (tb != null)
tb.Text = "";
I agree,
Casting using "As" will return null if cast fails instead of an
invalidcastexception.
Why is that good though? If I've got something which *should* be a
TextBox, why is it good to mask the fact that my assumption is wrong?
There are times when it's better to use "as" - namely if your code
really doesn't know whether the cast should work or not - and there are
times when it's better to use a cast.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too