From the course: Visual Basic Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Understand variable scope

Understand variable scope - Visual Basic Tutorial

From the course: Visual Basic Essential Training

Start my 1-month free trial

Understand variable scope

- [Instructor] We haven't talked about variable scope or lifetime yet. So, I'll do that in this RunCode method. I'll start by talking about local variables meaning variables that are declared within a sub-routine or function. The scope of a variable determines its visibility to other code. So the visibility of a local variable like this one declared here on line 22 would be inside this sub. I can't use it before I declare it but once I've declared it, I can use it anywhere else in this sub-routine. I'm using it here on line 27, and on line 34. I'm also incrementing the value over here on line 28. So I can use it all the way down until I get to the End Sub. Rule number two, variables in the same scope cannot have the same identifier or name. Let's look at line 23. I'll uncomment this line. I get an error. So even though this is a different type and a different initialized value, because it has the same…

Contents