|
-
Sep 14th, 2012, 04:34 PM
#1
Thread Starter
Member
Ignore errors in Private Sub
My Code Is:
Code:
Public Class Form1
Dim FreeSpace As String = "Panel16.Location"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim button As String = "button1.location"
Dim panel As String = "panel1.location"
Call MoveButton()
End Sub
Private Sub MoveButton()
button = FreeSpace
End Sub
End Class
I get an error in my private sub named "MoveButton" because "button" is not declared. I didn't Dim "button" in that sub because when I use the Call command in another sub, I want to use what "button" is "dim" there. How do i ignore the error in the private sub "MoveButton"? Thanks..
-
Sep 14th, 2012, 05:15 PM
#2
Re: Ignore errors in Private Sub
You don't ever ignore an error. It means you've got it wrong. It won't run or if it does it'll either crash or worse!
Firstly, don't use variable names that make no sense. You shouldn't use 'button' and 'panel' in the first place because it's confusing enough when the Type name is the same as one of your variables but to use it for something that isn't in fact a button or a panel is just plain silly.
Secondly the value of any variable declared within a Sub is available to that Sub only. Them's the rules and you just can't ignore them.
And finally, even if everything does run all this program does is to assign a String Variable with a value which could be achieved in one line ...
vb.net Code:
Dim button As String = "Panel16.Location"
... and is completely without function. You might just as well make button = "jam on toast" or "I Heart NY"
I'm not going to write this for you because frankly if you don't understand what's wrong there's really no point in your continuing with VB. I will give you a hint, however, ....
vb.net Code:
Private Sub MoveButton() 'those parentheses look terribly empty!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 14th, 2012, 11:09 PM
#3
Re: Ignore errors in Private Sub
Yes, you should not ignore errors, errors mean that somethign is wrong and ignoring them will not make them go away.
If you need to dim an object in one routine and use that same object in another routine then the common way is to pass a reference to the object as a parameter to the routine where you need to use it. The other method is to Dim the object at the top of the code so as to make it visible to the entire class but this method should be used only when the logic requires it. Passing as a parameter will work in most cases.
-
Sep 15th, 2012, 01:57 AM
#4
Re: Ignore errors in Private Sub
I didn't Dim "button" in that sub because when I use the Call command in another sub, I want to use what "button" is "dim" there.
To achieve that, you have to pass that variable ByRef not ByVal, Ex
Code:
Private Sub Sub1()
Dim strButton As String = "Initial value"
Call Sub2(strButton)
MessageBox.Show(strButton) ' this will show "A new value assigned in the Sub2" because the variable passed ByRef to the Sub2
End Sub
Private Sub Sub2(ByRef v As string)' if you replace ByRef with ByVal, the value of strButton will not changed in he caller sub (Sub1)
v = "A new value assigned in the Sub2"
End Sub
-
Sep 15th, 2012, 02:12 AM
#5
Re: Ignore errors in Private Sub
 Originally Posted by dunfiddlin
vb.net Code:
Private Sub MoveButton() 'those parentheses look terribly empty!
I see what you are doing there Dunfiddlin, and of course, I agree with your methods. However, you have already noted that the OP has very little idea of programming VB. A further clue such as would be better to give them a clue for what to search Google for.
EricJohanssen, I hope you follow Dunfiddlin's clue and my expansion of it and do not just use 4x2y's code, say "Wow, it works!" and leave it at that. Investigate ByVal and ByRef and see how and why they work the way they do.
As an alternative, you can define the variable button (It has already been mentioned another name would be much MUCH better) outside the sub as a global variable. It would then be available to all the subs in the class.
-
Sep 15th, 2012, 02:20 AM
#6
Re: Ignore errors in Private Sub
 Originally Posted by españolito
do not just use 4x2y's code, say "Wow, it works!" and leave it at that. Investigate ByVal and ByRef and see how and why they work the way they do.
The example i gave uses generic code and sub names not his original names in order to not just Copy & Paste
-
Sep 15th, 2012, 02:36 AM
#7
Re: Ignore errors in Private Sub
Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?
@Op
What are you really trying to do ?
-
Sep 15th, 2012, 04:54 AM
#8
Thread Starter
Member
Re: Ignore errors in Private Sub
 Originally Posted by Niya
Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?
@Op
What are you really trying to do ?
I was trying to make a slider puzzle game, but the way I was planning it in my head was probably a really bad and hard way of doing it. Firstly, the "panel" isn't really a panel, it's a picture box. The reason I can't define the button and "panel" above the class is because I want to use the call event in the click events for every buttons(1–8) and in each sub and define what "button" and "panel " means. So for button1, when using the call event, "button" should be "translated" to "button1. Location"... And "FreeSpace" Would be the location where a button could be moved.
If someone could help me with this then maybe my silly idea will work, and if it's a completely retarded idea, please tell me
-
Sep 15th, 2012, 05:11 AM
#9
Re: Ignore errors in Private Sub
You can use the same sub to handle click event for various button then use sender parameter to know which button fires the event, Ex
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click ' Add up to 8
Dim btnSender As Button = DirectCast(sender, Button)
MoveButton(btnSender)
End Sub
Private Sub MoveButton(ByVal btn As Button)
MessageBox.Show("You have clicked on the " & btn.Text)
btn.Left = btn.Left + 4
End Sub
-
Sep 15th, 2012, 05:13 AM
#10
Re: Ignore errors in Private Sub
-
Sep 15th, 2012, 07:41 AM
#11
Re: Ignore errors in Private Sub
 Originally Posted by Niya
Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?
Ahem ....
and is completely without function. You might just as well make button = "jam on toast" or "I Heart NY"
Just saying is all!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 15th, 2012, 07:45 AM
#12
Re: Ignore errors in Private Sub
Your crafty use of the English language takes getting used to
-
Sep 16th, 2012, 03:27 AM
#13
Junior Member
Re: Ignore errors in Private Sub
Thank you
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
|