-
Aug 18th, 2018, 05:48 AM
#1
Thread Starter
Hyperactive Member
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!
-
Aug 18th, 2018, 07:19 AM
#2
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.
-
Aug 18th, 2018, 11:17 AM
#3
Thread Starter
Hyperactive Member
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
-
Aug 18th, 2018, 11:33 AM
#4
Re: Change Signature question
Originally Posted by SteveHi
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.
-
Aug 18th, 2018, 11:42 AM
#5
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:
Protected Sub btnReg_Click(ByVal e As System.EventArgs, ByVal sender As Object) Handles btnReg.Click
to this:
vb.net Code:
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:
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.
-
Aug 19th, 2018, 12:02 PM
#6
Thread Starter
Hyperactive Member
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:
Thanks again
Steve
-
Aug 19th, 2018, 12:04 PM
#7
Thread Starter
Hyperactive Member
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:
Thanks again
Steve
-
Aug 19th, 2018, 12:16 PM
#8
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.
-
Aug 19th, 2018, 12:49 PM
#9
Thread Starter
Hyperactive Member
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!
-
Aug 19th, 2018, 12:52 PM
#10
Thread Starter
Hyperactive Member
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!
-
Aug 19th, 2018, 06:36 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|