Results 1 to 11 of 11

Thread: Object X = Y as X...Explain? [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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]
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Object X = Y as X...Explain?

    Gotcha with your first post jmcilhinney...could not make it more clear.

    Thanks for the replies...
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Object X = Y as X...Explain?

    Quote 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!!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Object X = Y as X...Explain? [RESOLVED]

    Quote Originally Posted by jmcilhinney
    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 thanks

    and cheer up
    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!!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object X = Y as X...Explain? [RESOLVED]

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Object X = Y as X...Explain? [RESOLVED]

    Quote 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!!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width