Popular Posts

Saturday, March 21, 2009

Statements and Scope

Statements and Scope

Statements

A statement is a complete instruction. It can contain keywords, operators, variables, literals, expressions and constants. Each statement in Visual Basic should be either a declaration statement or a executable statement. A declaration statement is a statement that can create a variable, constant, data type. They are the one's we generally use to declare our variables. On the other hand, executable statements are the statements that perform an action. They execute a series of statements. They can execute a function, method, loop, etc.

Option Statement

The Option statement is used to set a number of options for the code to prevent syntax and logical errors. This statement is normally the first line of the code. The Option values in Visual Basic are as follows.

Option Compare: You can set it's value to Text or Binary. This specifies if the strings are compared using binary or text comparison operators.
Option Explicit: Default is On. You can set it to Off as well. This requires to declare all the variables before they are used.
Option Strict: Default is Off. You can set it to On as well. Used normally when working with conversions in code. If you want to assign a value of one type to another then you should set it to On and use the conversion functions else Visual Basic will consider that as an error.

Example of Option Statement

The following code demonstrates where to place the Option statement.

Option Strict Off
Imports System
Module Module 1

Sub Main ()
Console.WriteLine (“Using Option”)
End Sub

End Module

The following code throws an error because Option Strict is On and the code attempts to convert a value of type double to integer.

Option Strict On
Imports System.Console
Module Module2

Sub Main()
Dim i As Integer
Dim d As Double = 20.12
i = d
WriteLine(i)
End Sub

End Module

We always should program with Option Strict On. Doing so allows us to catch many errors at compile time that would otherwise be difficult to track at run time.

Imports Statement

The Imports statement is used to import namespaces. Using this statement prevents you to list the entire namespace when you refer to them.

Example of Imports Statement

The following code imports the namespace System.Console and uses the methods of that namespace preventing us to refer to it every time we need a method of this namespace.

Imports System.Console
Module Module1

Sub Main()
Write("Imports Statement")
WriteLine("Using Import")
End Sub

End Module

The above two methods without an imports statement would look like this: System.Console.Write("Imports Statement") and System.Console.WriteLine("Using Import")

With Statement

With statemnt is used to execute statements using a particular object. The syntax looks like this:

With object
[statements]
End With

Sample Code

The following code sets text and width for a button using the With Statement.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
With Button1
.Text = "With Statement"
.Width = 150
End With
End Sub

Boxing

Boxing is implicit conversion of value types to reference types. Recall that all classes and types are derived from the Object class. Because they are derived from Object class they can be implicitly converted to that type. The following sample shows that:

Dim x as Integer=20
'declaring an integer x
Dim o as Object
'declaring an object
o=x
converting integer to object

Unboxing is the conversion of a boxed value back to a value type.

Scope

The scope of an element in code is all the code that can refer to it without qualifying it's name. Stated other way, an element's scope is it's accessibility in code. Scope is normally used when writing large programs as large programs divide code into different classes, modules, etc. Also, scope prevents the chance of code referring to the wrong item. The different kinds of scope available in VB .NET are as follows:

Block Scope: The element declared is available only within the code block in which it is declared.
Procedure Scope: The element declared is available only within the procedure in which it is declared.
Module Scope: The element is available to all code within the module and class in which it is declared.
Namespace Scope: The element declared is available to all code in the namespace.

No comments:

Post a Comment