Results 1 to 11 of 11

Thread: Change Signature question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Change Signature question

    Hello

    I have the following line of code in my VS 2017 Web page project:

    Code:
    Protected Sub btnReg_Click(ByVal e As System.EventArgs, ByVal sender As Object) Handles btnReg.Click
    However, a get a yellow light bulb that appears and suggests I change the above code to:

    Code:
    Protected btnReg_Click(sender As Object, e As EventArgs)
    or

    Code:
    Protected btnReg_Click(e As EventArgs, sender As Object)
    These two suggestions appear in a 'Change Signature' dialogue box.

    Which would be the best option, please?

    Thanks!

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

    Re: Change Signature question

    If that is supposed to be a Click event handler for a Button then you need to swap the parameters around. The 'sender' is ALWAYS first and 'e' is ALWAYS second in event handlers.

    Also, why is it declared Protected instead of Private? You shouldn't be calling event handlers directly so there's no good reason for that, as far as I can tell.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Change Signature question

    Thanks jmcilhinney for your explanation.

    When I replace

    Code:
    Protected Sub btnReg_Click(ByVal e As System.EventArgs, ByVal sender As Object) Handles btnReg.Click
    with either

    Code:
    Private btnReg_Click(sender As Object, e As EventArgs)
    or

    Code:
    Protected btnReg_Click(sender As Object, e As EventArgs)
    I get various errors

    Attachment 161085

    so I thought I had better ignore the light bulb and retain the code that I originally had.

    When you write

    you need to swap the parameters around
    do you mean the order of the parameters here:

    Code:
    cmd.Parameters.AddWithValue("@username", username.Text)
                    cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                    cmd.Parameters.AddWithValue("@Hash", "Production based on simple ciphers and fixed encryption keys")
    In my MS Access db, the three columns are in the order (from left to right): username, strEmail, Hash.

    Thanks again for your help.

    Regards

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Change Signature question

    Quote Originally Posted by SteveHi View Post
    I get various errors

    Attachment 161085

    so I thought I had better ignore the light bulb and retain the code that I originally had.
    No, you should fix the errors... because your existing code is wrong.

    Unfortunately you decided to not give any indication of what the errors are, so we can't help you fix them. I assume at least one is caused by the fact you missed out the very important word Sub (and it is probably causing all the other errors).

    However, based on the samples of code in your post, what you should probably have for that line is this:
    Code:
    Private Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReg.Click

    When you write
    you need to swap the parameters around
    do you mean the order of the parameters here:

    Code:
    cmd.Parameters.AddWithValue("@username", username.Text)
                    cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                    cmd.Parameters.AddWithValue("@Hash", "Production based on simple ciphers and fixed encryption keys")
    In my MS Access db, the three columns are in the order (from left to right): username, strEmail, Hash.
    No, he was referring to the parameters to the Sub, which is the part inside the brackets.

    In terms of cmd.parameters, the order you have them is not related to the order of items in the database, but the order of the items in your SQL statement. Based on the little I can see in the screenshot, they seem to be correct.
    Last edited by si_the_geek; Aug 18th, 2018 at 11:37 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Change Signature question

    Given that I referred explicitly to 'sender' and 'e', it should have been obvious that those were the parameters I was referring to; the method parameters. All you need to do was what I said, i.e. change this:
    vb.net Code:
    1. Protected Sub btnReg_Click(ByVal e As System.EventArgs, ByVal sender As Object) Handles btnReg.Click
    to this:
    vb.net Code:
    1. Protected Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReg.Click
    That said, I'd have it like this:
    vb.net Code:
    1. Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
    There's generally no good reason for an event handler to not be Private, the ByVal keywords are superfluous and the qualifying namespace on EventArgs is also superfluous.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Change Signature question

    Thank you Si the Geek

    I have replaced what I had just before I read your reply, which was:

    Code:
    Private Sub btnReg_Click(sender As System.Object, e As System.EventArgs) Handles btnReg.Click
    with your:

    Code:
    Private Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReg.Click
    Again that yellow light bulb pops up with the 'Change Signature' dialogue box:

    Name:  SiTheGeek.jpg
Views: 190
Size:  26.1 KB

    Thanks again

    Steve

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Change Signature question

    Thank you Si the Geek

    I have replaced what I had just before I read your reply, which was:

    Code:
    Private Sub btnReg_Click(sender As System.Object, e As System.EventArgs) Handles btnReg.Click
    with your:

    Code:
    Private Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReg.Click
    Again that yellow light bulb pops up with the 'Change Signature' dialogue box:

    Name:  SiTheGeek.jpg
Views: 190
Size:  26.1 KB

    Thanks again

    Steve

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Change Signature question

    IT looks as though the suggestion is just to remove the System prefix from the two data types, if you have imported the System namespace (either at the top of the file or in the project settings) you don't need to qualify the types declared in that namespace.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Change Signature question

    Thanks PlausiblyDamp

    Yes, I have the System namespace and used jmcilhenny's suggestion of

    Code:
    Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
    Cheers to you all!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Change Signature question

    Thanks PlausiblyDamp

    Yes, I have the System namespace and used jmcilhenny's suggestion of

    Code:
    Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
    Cheers to you all!

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

    Re: Change Signature question

    Note that, when the forum tells you that you need to wait 29 seconds (or thereabouts) to post again, it actually has submitted your post. If you scroll down the page a little you can see that. You don't actually need to try to post a second time or you'll end up double-posting, as you've done here a couple of times. It seems to happen when including CODE or HIGHLIGHT tags and maybe QUOTE tags too. Maybe any tags, although I haven't tested that.

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