|
-
Sep 6th, 2005, 11:21 PM
#1
Thread Starter
Frenzied Member
Object X = Y as X...Explain? [RESOLVED]
Can someone explain this simple code? First time I see it in a C# example
Button btn = sender as Button;
if (btn != null)
Last edited by StrangerInBeijing; Sep 7th, 2005 at 12:29 AM.
Reason: [RESOLVED]
-
Sep 6th, 2005, 11:59 PM
#2
Re: Object X = Y as X...Explain?
It's just converting from one type to another compatible type. The result is basically the same as:
Code:
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).
-
Sep 7th, 2005, 12:14 AM
#3
Re: Object X = Y as X...Explain?
it doens't use a catch block
it's more like this, I cant remember EXACTLY, but it's very close:
Code:
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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 7th, 2005, 12:25 AM
#4
Re: Object X = Y as X...Explain?
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.
-
Sep 7th, 2005, 12:28 AM
#5
Thread Starter
Frenzied Member
Re: Object X = Y as X...Explain?
Gotcha with your first post jmcilhinney...could not make it more clear.
Thanks for the replies...
-
Sep 7th, 2005, 12:32 AM
#6
Re: Object X = Y as X...Explain?
 Originally Posted by jmcilhinney
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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 7th, 2005, 12:41 AM
#7
Re: Object X = Y as X...Explain? [RESOLVED]
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.
-
Sep 7th, 2005, 12:45 AM
#8
Re: Object X = Y as X...Explain? [RESOLVED]
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 7th, 2005, 12:51 AM
#9
-
Sep 7th, 2005, 01:18 AM
#10
Re: Object X = Y as X...Explain? [RESOLVED]
 Originally Posted by jmcilhinney
You can consider me suitably cheery.  There was a lack of smilies in my previous post but I was still a'smlin'.  I just want to be cheerful and right. 
hehehehe
perhaps I use too many smilies
umm I'm stretching this thread a bit, but do you know if a similar statement exists in vb.net or not?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 7th, 2005, 01:39 AM
#11
Re: Object X = Y as X...Explain? [RESOLVED]
Not that I'm aware of, but then I don't know everything, no matter how hard I try to pretend I do.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|