Answer to Question #50436 in Visual Basic for Bender

Question #50436
Give an example of when a variable is scoped to a block of code. Explain why this is good programming practice.
1
Expert's answer
2015-01-21T02:52:56-0500
Answer:
When created in this way, the scope of thevariable is the entire method after the declaration. This means that the
variable is available to use within the method but when control passes to
another method the variable is unavailable.
There are other possibilities for a variable'sscope. For example, a variable can be declared within a loop or other code
structure and be only visible to the code within the structure. A wider scoped
variable could be declared at class-level so that it can be used by any method
within the class. In fact, a variable's scope is always the full extent of the
code block it is declared within.
Code blocks can be nested. A loop within amethod within a class provides three levels of nested code blocks and,
therefore, three levels of nested scope. When a variable is declared within one
of these nested scopes, it is visible only to the current scope and any scopes
nested within it. This means that a variable declared within a loop is not
visible outside of that loop whereas a variable declared outside of the loop is
also visible within the loop.
Class-Level Scope: Variables that are defined at the class level are available to anynon-static method within the class.
Method-Level Scope: Variables declared within a method's code block are available for use byany other part of the method, including nested code blocks. The following
example demonstrates this by creating a variable in the method, setting its
value and then using it within the scope of an 'if' statement.
[table]

static void Main(string[] args)
{
int score; // Declared at method-level
score = 100;
// Used at method-level
if (score >= 50)
Console.WriteLine("Good score : {0}", score); // Used in nested scope
else
Console.WriteLine("Poor score : {0}", score); // Used in nested scope
}


[/table]Variables declared within a nested scope are notavailable to those outside of their code block. The following code will not
compile because the variable that is attempted to be used at method level is
declared in the more deeply nested scope of the if statement.
[table]

static void Main(string[] args)
{
int score = 100;
if (score >= 50)
string message = "Good
score";
// Declared in if statement
else
string message = "Poor
score";
// Declared in if statement
Console.WriteLine(message); // Variable unavailable
}









In general, when declaring any variable or constant, it is good programming
practice to make the scope as narrow as possible (block scope is the
narrowest). This helps conserve memory and minimizes the chances of your code
erroneously referring to the wrong variable.


[/table]

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS