Review Questions


1:

What is meant by the scope of a variable?

A1:

The scope of a data item refers to the accessibility of the data item. For example, if you define a variable within a procedure, such as a subroutine, you can only access that variable within the subroutine in which it is defined. Therefore, the scope of the variable is the subroutine in which it is defined.

2:

What is meant by the lifetime of a variable?

A2:

Normally, the lifetime of a variable is the same as its scope. That is, the lifetime of a variable starts with its definition and ends when the variable goes out of scope. Therefore, for normal variables defined within procedures, the lifetime starts when the program control enters the subroutine and ends when program control exits the subroutine. A Static variable is an exception: Its lifetime is equal to that of the program.

3:

What does the Static keyword do to the attributes of a variable?

A3:

When a variable is defined with the Static keyword, Visual Basic .NET tags the variable with a special attribute that says that the lifetime of the variable starts when the program begins execution and ends when the program terminates. This is true even if the scope of the variable is limited to a subroutine.

4:

What are the different scope levels?

A4:

The different scope levels are local (or procedure) scope, block scope, module scope, and namespace scope.

5:

Which scope level is best to use in a program?

A5:

Each scope level has a purpose, and one size does not fit all. However, as you move up the scope scale and expose a data item to larger and larger sections of the program, the chance of contaminating the data increases. This risk increases as you increase the exposure of the data item to other sections of the program. Therefore, all things being equal, the more restrictive the scope level, the better, because you are hiding the data and more effectively protecting the data from outside contamination.

6:

Say you want to display the number of times a certain procedure named CountHits() is entered. The name of the form that needs this feature is frmMain . How would you write the code to display the number of times CountHits() is entered on the title bar of the form?

A6:

The following might be one solution:

 Private Sub CountHits()   Static MyCounter as Long  MyCounter += 1  frmMain.Text = "Hits: " & CStr(MyCounter)  ' Whatever other code might be in the procedure End Sub 

This code defines MyCounter as a Static data type, so its lifetime equals that of the program. Therefore, its value is initialized only once, at program startup. After that, the definition statement is never executed again, and its value persists until the program ends.

7:

If a data item is defined with local scope and it is entered 12 times while the program is running, what is displayed on the title bar of the form when you use the following code?

 Private Sub ThisProc()   Dim i as Integer  i += 1  Me.Text = CStr(i)  ' Other statements End Sub 
A7:

The number displayed will always be 1 . Because the variable has local scope, it is initialized to each time the procedure is entered. Because it is then incremented by 1, the value 1 is always displayed.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net