Results 1 to 5 of 5

Thread: Things I haven't noticed in VB until now

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Things I haven't noticed in VB until now

    Just want to share a couple of discoveries I've just made (for myself) about visual basic.

    1. Did you know that you don't need to specify that boring
    Code:
    sender As System.Object, e As System.EventArgs
    in the event handlers any more if you don't need the sender part.

    I just wrote

    vb Code:
    1. Private Sub Form1_Load() Handles Me.Load
    2.         MessageBox.Show("test")
    3.     End Sub

    and it was compiled without. Imagine that!

    2. What I missed the most is the true conditional operator (like the one in my signature). IIf was a poor substitution since it is really a function, not a ternary operator and it evaluated all its arguments which defeated the initial purpose of this language construction. To my amazement, I only just discovered the If operator which happens to be just that - a ternary operator. So, you can simply do this:

    vb Code:
    1. TextBox1.Text = If(IsDbNull(value), "", value.ToString())

    Perhaps I didn't tell anything new to someone, but I'm sure, there are people who might find this information useful.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Things I haven't noticed in VB until now

    Cool, I didn't know about number 1. How does that even work? Is it a new feature in VS 2010 or something? I'm pretty sure it used to error on that in the past... (incompatible event signature)

  3. #3

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Things I haven't noticed in VB until now

    Somehow, I think that two delegates are invoked on each such event. One is with the usual (sender As Object, e As EventArgs) and the second one is without it.

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

    Re: Things I haven't noticed in VB until now

    The If operator certainly is useful in various cases but I should point out that this:
    vb.net Code:
    1. TextBox1.Text = If(IsDbNull(value), "", value.ToString())
    is redundant because DBNull.ToString returns an empty string anyway.
    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
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Things I haven't noticed in VB until now

    Quote Originally Posted by jmcilhinney View Post
    The If operator certainly is useful in various cases but I should point out that this:
    vb.net Code:
    1. TextBox1.Text = If(IsDbNull(value), "", value.ToString())
    is redundant because DBNull.ToString returns an empty string anyway.
    A poor example, perhaps...

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