Variable and Structure Control in VB.NET

A variable can be defined as an entity that has the following six properties:

  • Name
  • Address
  • Type
  • Value
  • Scope
  • Lifetime

Variable Scope

Variables (and constants) have a scope, which indicates where in the program the variable is

recognized or visible to the code, that is, where it can be referred to in code.

Local variables: block-level and procedure-level scope

If a variable is declared inside a code block (a set of statements that is terminated by an End...,Loop, or Next statement), then the variable has block-level scope ; that is, it is visible only within that block.

For example, consider the following code:

If x <> 0 Then

Dim rec As Integer

rec = 1/x

End If

MsgBox CStr(rec)

In this code, the variable rec is not recognized outside the block in which it is defined, so the final statement produces an error.

Module-level and project-level scope

We first note that a standard module itself can be declared using one of the access modifiers Public, Friend, or Private (this is the default). Using such a modifier simply restricts the individual members to that level of access at most. Thus, for instance, a Public variable declared in a Friend module has only Friend scope.

Private access

A variable declared in the Declarations section of a standard module using the Private access modifier has module-level scope; that is, it is visible in the entire module, but nowhere else. Using the Dim keyword also gives the variable module-level scope, but its use is not as clear and should be avoided for readability sake.

Friend access

A variable declared in the Declarations section of a standard module using the Friend access modifier is visible in the entire project and thus has project-level scope. However, it is not visible to other projects.

Public access

A variable declared in the Declarations section of a Public standard module using the Public access modifier is visible not only to the project in which it is declared, but also to any external project that holds a reference to the project. For instance, consider the following module declared in Project1:

Public Module Module1

Public iModulePublic As Integer

Friend iModuleFriend As Integer

End Module

If Project2 has a reference to Project1, then we can write:

Project1.Module1.iModulePublic = 100

However, the code:

Project1.Module1.iModuleFriend = 100

generates a "not accessible" syntax error.

Declaring Variables and Constants

A variable declaration is an association of a variable name with a data type. In and of itself, this does not imply variable creation. However, for nonobject variables, a variable declaration does create a variable. A declaration such as:

Dim x As Integer

creates an Integer variable named x. We can also write:

Dim x As Integer = New Integer( )

which emphasizes the role of the constructor function for the Integer data type. (The constructor is the function that VB .NET uses to create the variable.)

When multiple variables are declared on the same line, if a variable is not declared with an explicit type declaration, then its type is that of the next variable with an explicit type declaration. Thus, in the line:

Dim x As Long, i, j, k As Integer, s As String

the variables i, j, and k have type Integer. (In VB 6, the variables i and j would have type Variant, which is VB 6's default data type.)

VB .NET permits the initialization of variables in the same line as their declaration (at long last!). Thus, we may write:

Dim x As Integer = 5

to declare an Integer variable and initialize it to 5. Similarly, we can declare and initialize more than one variable on a single line:

Dim x As Integer = 6, y As Integer = 9

Note that in this case, each variable that you declare must explicitly be assigned a data type. You cannot assign each variable an explicit value without explicitly declaring the data type of each variable.

Data Types

Value and Reference Types

The types defined in the CTS fall into three categories:

  • Value types
  • Reference types
  • Pointer types

However, pointer types are not implemented in VB, so we will not discuss these types. The difference between value and reference types is how variables of the corresponding type represent that type. When a value-type variable is defined, as in:

Dim int As Integer = 5

a memory location is set aside to hold the actual data (in this case the number 5). In contrast, when a reference-type variable is defined, as in:

Dim obj As New CEmployee

the VB compiler creates the object in memory, but then sets the variable obj to a 4-byte memory location that contains the address of the object.

In short, value-type variables contain the data, whereas reference-type variables point to the data. The following lists the data types supported by VB .NET, along with their underlying .NET type, storage requirements, and range of values:

Simple Data Types in Visual Basic

  • Numeric data type
  • Integer data type
  • Floating-point data type
  • Boolean data type
  • Character data type

2. Arithmetic Operator

Operator

Example

Description

+

Net + Disc

Adds two values

-

Price - 4.00

Subtracts one value from another value

*

Total * Fact

Multiplies two values

/

Tax / Adjust

Divides one value by another value

^

Adjust ^ 3

Raises a value to a power

& (or +)

Name1 & Name2

Concatenates two strings

3. Logic Operator

Operator

Usage

Description

And

If (A > B) And (C <>

Produces True if both sides of the And are true. Therefore, A must be greater than B and C must be less than D. Otherwise, the expression produces a false result.

Or

If (A > B) Or (C <>

Produces True if either side of the Or is true. Therefore, A must be greater than B or C must be less than D. If both sides of the Or are false, the entire expression produces a false result.

Not

If Not(strAns = "Yes")

Produces the opposite true or false result. Therefore, if strAns holds "Yes", the Not turns the true result to false.

4. Selection

The If Statement

If makes decisions. If a comparison test is true, the body of the If statement executes. (In fact, the previous sentence is almost identical to Visual Basic's If statement!) Here is one format of If:

If comparisonTest Then

One or more Visual Basic statements

End If

Example :

If (intUnitsSold > 10000) Then

sngBonus = 50.00

End If

The If Statements Else Branch

If comparisonTest Then

One or more Visual Basic statements

Else

One or more Visual Basic statements

End If

Example :

If (intDivNum = 3) Or (intDivNum = 4) Then

curDivTotal = curDivSales3 + curDivSales4

curGrandDivCosts = (curDivCost3 * 1.2) + (curDivCost4 * 1.4)

Else

curDivTotal = curDivSales1 + curDivSales2

curGrandDivCosts = (curDivCost1 * 1.1) + (curDivCost5 * 1.9)

End If

Multiple Choice with Select Case

Select Case Expression

Case value

One or more Visual Basic statements

Case value

One or more Visual Basic statements

[Case value

One or more Visual Basic statements]

[Case Else

One or more Visual Basic statements]

End Select

Example :

Select Case intAge

Case 5: lblTitle = "Kindergarten"

Case 6: lblTitle = "1st Grade"

Case 7: lblTitle = "2nd Grade"

Case 8: lblTitle = "3rd Grade"

Case 9: lblTitle = "4th Grade"

Case 10: lblTitle = "5th Grade"

Case 11: lblTitle = "6th Grade"

Case Else: lblTitle = "Advanced"

End Select

Two Additional Select Case Formats

Select Case Expression

Case Is Relation:

One or more Visual Basic statements

Case Is Relation:

One or more Visual Basic statements

[Case Is Relation:

One or more Visual Basic statements]

[Case Else:

One or more Visual Basic statements]

End Select

Select Case Expression

Case expr1 To expr2:

One or more Visual Basic statements

Case expr1 To expr2:

One or more Visual Basic statements

[Case expr1 To expr2:

One or more Visual Basic statements]

[Case Else:

One or more Visual Basic statements]

End Select

Example :

Select Case intAge

Case Is <5: lblTitle.Text = "Too young"

Case 5: lblTitle.Text = "Kindergarten"

Case 6 To 11: lblTitle.Text = "Elementary"

lblSchool.Text = "Lincoln"

Case 12 To 15: lblTitle.Text = "Intermediate"

lblSchool.Text = "Washington"

Case 16 To 18: lblTitle.Text = "High School"

lblSchool.Text = "Betsy Ross"

Case Else: lblTitle.Text = "College"

lblSchool.Text = "University"

End Select

5. Procedure

Sub procedure_name(parameter)

statement

End sub

6. Function

Function function_name(parameter) as typedata

Statement

End sub

7. Passing Parameter by Value and Reference

Visual Basic lets you pass arguments two ways: by reference and by value. The way you use them determines whether the receiving procedure can change the arguments so that those changes remain in effect after the calling procedure regains control. If you pass and receive by reference (the default method), the calling procedure's passed local variables may be changed in the receiving procedure. If you pass and receive by value, the calling procedure can access and change its received arguments, but those changes don't retain their effects in the calling procedure

Sub Changes (N As Integer, S As Single)

` Receives arguments by reference

N = N * 2 ` Double both

S = S * 2 ` arguments

` When the calling routine regains control,

` its two local variables will now be twice

` as much as they were before calling this.

End Sub

Sub NoChanges (ByVal N As Integer, ByVal S As Single)

` Receives arguments by value

N = N * 2 ` Double both

S = S * 2 ` arguments

` When the calling routine regains control,

` its two local variables will not be

` changed from their original values

End Sub

8. Data Type Conversion

The process of converting a value of one data type to another is called conversion or casting. A cast operator can be applied to a literal value or to a variable of a given type. For instance, we have:

Dim lng As Long

Dim int As Integer = 6

' Cast an Integer variable to a Long

lng = CLng(Int)

' Cast a literal integer to a Long

lng = CLng(12)

where expression is an expression that is in the range of the target data type. Specifically, we have the following conversion functions:

  • CBool
  • CByte
  • CChar
  • CDate
  • CDbl
  • CDec
  • CInt
  • CLng
  • CObj
  • CShort
  • CSng
  • CStr
  • CType

Members of the System.Convert class



Download PDF File