PDA

Click to See Complete Forum and Search --> : Object X = Y as X...Explain? [RESOLVED]


StrangerInBeijing
Sep 6th, 2005, 11:21 PM
Can someone explain this simple code? First time I see it in a C# example

Button btn = sender as Button;
if (btn != null)

jmcilhinney
Sep 6th, 2005, 11:59 PM
It's just converting from one type to another compatible type. The result is basically the same as:Button btn;

try
{
btn = (Button)sender;
}
catch (Exception ex)
{
btn = null;
}but the implementation is not quite the same. Check it out in the help, which is pretty much my first advice for everything. Just set the Filter to C# and search for "as" (without quotes).

MrPolite
Sep 7th, 2005, 12:14 AM
it doens't use a catch block
it's more like this, I cant remember EXACTLY, but it's very close:

btn = (sender is Button?
(Button)sender :
null


it's good cuz it reduces some code writing. Also the "is" operator lets you check types (it, that woudl be the same as doing if (btn.GetType() == typeof(Button))

catch block would be inefficient:D

jmcilhinney
Sep 7th, 2005, 12:25 AM
I only used that as an illustration in easily understandable terms. :) I did state that the implementation is not the same. The help topic uses the ternary if-else operator as an illustration also, because that is not the same either because the expression being converted is only evaluated once when using "as". Basically, it is an operator and it can't necessarily be equated to any C# code because it is part of the language itself.

StrangerInBeijing
Sep 7th, 2005, 12:28 AM
Gotcha with your first post jmcilhinney...could not make it more clear. :)

Thanks for the replies...

MrPolite
Sep 7th, 2005, 12:32 AM
I only used that as an illustration in easily understandable terms. :) I did state that the implementation is not the same. The help topic uses the ternary if-else operator as an illustration also, because that is not the same either because the expression being converted is only evaluated once when using "as". Basically, it is an operator and it can't necessarily be equated to any C# code because it is part of the language itself.yeah I didnt read your post properly
but bleh MSDN says they are the same

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:
expression as type
is equivalent to:

expression is type ? (type)expression : (type)null


:afrog:

jmcilhinney
Sep 7th, 2005, 12:41 AM
You omitted the line that follows that:except that expression is evaluated only once.which implies that, as for my example, the result is the same but the route taken to get there is different.

MrPolite
Sep 7th, 2005, 12:45 AM
You omitted the line that follows that:which implies that, as for my example, the result is the same but the route taken to get there is different.
I see, I did miss it:afrog: thanks

and cheer up:D:D:D

jmcilhinney
Sep 7th, 2005, 12:51 AM
You can consider me suitably cheery. :D There was a lack of smilies in my previous post but I was still a'smlin'. :) I just want to be cheerful and right. :blush:

MrPolite
Sep 7th, 2005, 01:18 AM
You can consider me suitably cheery. :D There was a lack of smilies in my previous post but I was still a'smlin'. :) I just want to be cheerful and right. :blush:
hehehehe
perhaps I use too many smilies:D

umm I'm stretching this thread a bit, but do you know if a similar statement exists in vb.net or not?

jmcilhinney
Sep 7th, 2005, 01:39 AM
Not that I'm aware of, but then I don't know everything, no matter how hard I try to pretend I do. :)