|
-
Jun 16th, 2019, 02:24 PM
#23
Re: System.InvalidOperationException in Application designer.vb
 Originally Posted by Rocky48
So, if I'm correct the form is a Module, therefore any variables I declare in that module will be available to any of the sub-routines.
How does it change if in a ValueChanged event?
If you declare variables inside a routine (a sub or function), then the variable only exists inside that routine, and only keeps its value until the routine is exited (via End Sub or End Function etc).
In this case you want the value inside another routine, so you should have the Dim Fsize line inside that other routine. The control will still have the same value as it did when the ValueChanged event ran, so the variable will have the correct value, but it will be available in the place you want to use it.
The other NUD is similar, but says that nudTop has not been declared??
Code:
Private Sub nudTop_ValueChanged(sender As Object, e As EventArgs)
Dim CTop As Integer = CInt(nudTop.value)
End Sub
It sounds like the control isn't actually called nudTop, so go to the designer (the 'picture' of the form) and check the properties of the control to make sure the name is right.
As above, you also do not need to use this ValueChanged event, just create the variable inside the routine where you want to use the value.
I did try as InteliSense suggested, but that still said that nudTop had been used before it had been assigned a value?
Code:
Dim nudTop As NumericUpDown = nudTop
Don't create variables with the same name as a control, that will only cause you problems.
In terms of the automatic suggestions for fixing errors, while they can be right sometimes, they are often wrong - and can therefore make things worse (in this case, by adding a variable that serves no purpose, and is just adding confusion).
You should generally only use an automatic suggestion if you already understand it (and the circumstances of the error) well enough to be sure it will solve the problem. If you don't know enough to be sure then you can try it, but if it doesn't completely solve things, undo it and find the solution another way.
I have purchased a VB book, but it does not cover all the variations and nether does the material online.
Nothing will ever cover even 1% of the variations, because there are many billions of variations.
The important thing is that there are "building blocks", and you can put them together in various ways (like Lego).
A good tutorial or book will teach you the building blocks (such as variables, and controls, and loops), and they are the building blocks you will almost always use, so it is important to understand them properly. Unfortunately at the moment there are signs that you don't understand them well enough.
edit: it seems that quick break I had while writing was longer than I thought, and Inferrd got in before me (by quite a bit!). I disagree with his suggestion of declaring the variable at class level, because you don't actually need to use the variable outside of btnPrint_Click
Whenever possible, you should declare variables inside routines, as that helps avoid mistakes.
Tags for this Thread
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
|