Answered by:
[VB.NET] Declared variable appearing not declared?!

Question
-
The following is a screenshot of the problem:
What can I do?Thursday, February 24, 2011 6:13 PM
Answers
-
Hi Xakepa,
Declared variables in a if block statement make them local to the concerned if.
Try to make it "global" to the Sub by writing Dim bitlevel As Integer before the If.
Then just assign the value like : If bitl8.Checked = True Then bitlevel = 8
and so on.
And bitlevel will be accessible.
- Proposed as answer by Andrew B. Painter Thursday, February 24, 2011 7:11 PM
- Marked as answer by Calvin_Gao Thursday, March 3, 2011 9:57 AM
Thursday, February 24, 2011 6:20 PM -
I think instead of MOVING the declaration from the If to the Sub scope you COPIED the declaration so it still exists in both places. Declare it once at the Sub scope, then set its value inside the If scope.
For future reference, screenshots (especially redacted) are really rarely helpful to anyone who actually wants to help you - this one happened to be one in around a thousand. An actual post of your code allows users to copy and paste it into Visual Studio, see any Intellisense errors for themselves, and try debugging if necessary. I promise no one will mock you for using funny-looking variable names. Funny-smelling ones are another story.
It never hurts to try. In a worst case scenario, you'll learn from it.- Marked as answer by Calvin_Gao Thursday, March 3, 2011 9:57 AM
Thursday, February 24, 2011 7:10 PM
All replies
-
Hi Xakepa,
Declared variables in a if block statement make them local to the concerned if.
Try to make it "global" to the Sub by writing Dim bitlevel As Integer before the If.
Then just assign the value like : If bitl8.Checked = True Then bitlevel = 8
and so on.
And bitlevel will be accessible.
- Proposed as answer by Andrew B. Painter Thursday, February 24, 2011 7:11 PM
- Marked as answer by Calvin_Gao Thursday, March 3, 2011 9:57 AM
Thursday, February 24, 2011 6:20 PM -
Now it shows the error 'Variable 'bitlevel' hides a variable inside an enclosing block.'Thursday, February 24, 2011 6:38 PM
-
I think instead of MOVING the declaration from the If to the Sub scope you COPIED the declaration so it still exists in both places. Declare it once at the Sub scope, then set its value inside the If scope.
For future reference, screenshots (especially redacted) are really rarely helpful to anyone who actually wants to help you - this one happened to be one in around a thousand. An actual post of your code allows users to copy and paste it into Visual Studio, see any Intellisense errors for themselves, and try debugging if necessary. I promise no one will mock you for using funny-looking variable names. Funny-smelling ones are another story.
It never hurts to try. In a worst case scenario, you'll learn from it.- Marked as answer by Calvin_Gao Thursday, March 3, 2011 9:57 AM
Thursday, February 24, 2011 7:10 PM