2. Variable scope
• The declaration of a variable establishes more than just the name and
data type of the variable.
• Depending on the keyword used to declare the variable and the
placement of the variable’s declaration in the program’s code, the variable
might be visible to large portions of the application’s code.
• Alternatively, a different placement might severely limit where the
variable can be referenced in the procedures within the application.
• The visibility of a variable or procedure is called its scope. A variable that
can be seen and used by any procedure in the application is said to have
public scope. Another variable, one that is usable by a single procedure, is
said to have scope that is private to that procedure.
3. Private variables
• Private variables are available to the module, class, or structure in which they are
declared.
• As with previous Visual Basic version, Dim and Private act in the same manner,
with the exception that the Private keyword cannot be used to declare variables
within a subprocedure or function.
• Here is an example of how you could use Private and Dim within a class:
Public Class Var1
Private Shared intJ as Integer
Shared Sub Main()
Dim intY as Integer = 100
Call DoSomething
Console.Writeline(intJ)
' returns 100
Console.Writeline(intY)
End Sub
Private Shared Sub DoSomething()
IntJ = 100
End Sub
End Class
Sub EatDinner() as Boolean
Private intX as integer = 100
End Sub
The following code is invalid.
Remember, Private is not
allowed to declare local
variables within a
subprocedure or function.
4. Public variables
• Public variables are available to all
procedures in all classes, modules, and
structures in your application.
• It is important to note that in most
cases, method and variable declaration
is public by default in VB .NET. The
following example demonstrates the
use of a public variable used in multiple
classes.
• Notice that both intX and intY are in the
Public scope, and are accessible to the
classes in the module.
Module ModMain
' This is our public variable
Public intX, intY As Integer
Public Class Dog
Sub X()
intX = 100
End Sub
Sub Y()
intY = 300
End Sub
End Class
Private Class Animals
Sub Z()
intX = 500
End Sub
End Class
End Module
5. Static variables
• Static variables are special variable types that retain their values within the scope of the
method or class in which they are declared.
• A static variable retains its value until the value of the variable is reset, or until the application
ends.
• In this example, each time the sub is executed, the value of intS is retained until the value
reaches 105, then the value of intX is reset to 100, and this happens forever until the
application ends.
• Static variables can only be declared at the procedure level. If you attempt to declare a variable
at the class or module level, you get a compiler error.
Static intS As Integer = 100
Sub Test()
intS = intS + 1
If intS = 105 Then
intS = 100
End If
End Sub
6. Shared variables
• Shared members are properties, procedures, or fields
that are shared by all instances of a class.
• This makes it easy to declare a new instance of a class,
but maintain a shared, public variable throughout all
instances of the class.
• If you are expecting variables to be initialized to empty
or null when a class is instantiated, so you should use
caution when implementing shared variables
7. Protected variables
• Protected variables are only available to the class in
which they are declared, or classes that derive from
the same class. Module Module1
Class A
' Declare protected variable
Protected X As Integer
Private Function TestX() As Boolean
' Set value of protected variable
X = 5
End Function
End Class
Class B
Private Function TestX() As Boolean
X = 5
End Function
End Class
End Module
In the following code, the
variable X is available only to
the class in which it is declared.
The attempt to access it in the
Class B results in an error.
8. Friend variables
• Friend variables are accessible from any class or module within the
assembly that they are declared in.
• In the following code, a friend variable is declared at the module level, and
the variable is available to all classes within this module.
Module Module1
' Declare Friend variable
Friend X As Integer
Class A
Private Function TestX() As Boolean
' Set value of protected variable
X = 5
End Function
End Class
Class B
Private Function TestX() As Boolean
X = 5
End Function
End Class
End Module
9. Data Type Conversion
In Visual Basic, data can be converted in two
ways:
• implicitly, which means the conversion is
performed automatically,
• and explicitly, which means you must
perform the conversion.
10. Implicit Conversions
• An implicit conversion does not require any special syntax in the source code.
• Let's understand implicit conversions in code.
• The example below declares two variable, one of type double and the other integer. The
double data type is assigned a value and is converted to integer type.
• When you run the code the result displayed is an integer value, in this case the value
displayed is 132 instead of 132.31223.
Module Module1
Sub Main()
Dim d=132.31223 as Double
Dim i as Integer
i=d
WriteLine("Integer value is" & i)
End Sub
End Module
11. Explicit Conversions
• An explicit conversion also called as cast uses a type
conversion keyword.
• Visual Basic provides several such keywords, which coerce an
expression in parentheses to the desired data type.
• In the following example, the CInt keyword converts the value
of double back to an integer before assigning it to an integer.
Module Module1
Sub Main()
Dim intk As Integer
Dim dblq As Double
dblq=432
dblq=Math.Sqrt(dblq)
intk= CInt(dblq)
End Sub
End Module
12. Widening Conversions
Data type Widens to data types
Sbyte SByte, Short, Integer, Long, Decimal, Single, Double
Byte Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal,
Single, Double
Short Short, Integer, Long, Decimal, Single, Double
Ushort UShort, Integer, UInteger, Long, ULong, Decimal, Single,
Double
Integer Integer, Long, Decimal, Single, Double
Uinteger UInteger, Long, ULong, Decimal, Single, Double
Long Long, Decimal, Single, Double
Ulong ULong, Decimal, Single, Double
Decimal Decimal, Single, Double
Single Single, Double
Double Double
Char Char, String
13. Narrowing Conversions
The standard narrowing conversions include the following:
• The reverse directions of the widening conversions in the preceding table
(except that every type widens to itself)
• Conversions in either direction between Boolean and any numeric type
• Conversions in either direction between String and any numeric type,
Boolean, or Date
• Conversions from a data type or object type to a type derived from it.
Narrowing conversions do not always succeed at run time, and can fail or
incur data loss. An error occurs if the destination data type cannot receive
the value being converted. You use a narrowing conversion when you know
the source value can be converted to the destination data type without error
or data loss.
14. Conversion Keywords
Keyword Output data type Allowable data types
CLng Long data type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
CBool Boolean data type Any valid char or string or numeric expression
CByte Byte data type Any numeric type (including SByte and
enumerated types), Boolean, String, Object
CChar Char data type String, Object
CDate Date data type String, Object
CDbl Double data type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
CDec Decimal data type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
CInt Integer data type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
15. Keyword Output data type Allowable data types
CObj Object data type Any type
CSByte SByte Data Type Any numeric type (including Byte and enumerated
types), Boolean, String, Object
CShort Short Data Type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
CSng Single Data Type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, String, Object
CStr String Data Type Any numeric type (including Byte, SByte, and
enumerated types), Boolean, Char, Char array,
Date, Object
CType Type specified
following the
comma (,)
Conversion Keywords
16. The CType Function
Returns the result of explicitly converting an expression to a specified data
type, object, structure, class, or interface.
CType(expression, typename)
The CType Function operates on two arguments:
• The first (expression) is the expression to be converted,
• and the second (typename) is the destination data type or object class.
Note that the first argument must be an expression, not a type.
CType includes all of the VB conversion functions.
17. Value Changes During Conversions
• A conversion from a value type stores a copy of the source value in the
destination of the conversion. However, this copy is not an exact image of the
source value.
• The destination data type stores values differently, and even the value being
represented might change, depending on the kind of conversion being
performed.
• Narrowing conversions change the destination copy of the source value, with
potential information loss. (fractional value is rounded when converted to an
integral type)
• Widening conversions preserve the source value but can change its
representation. (Integral type to Decimal, or Char to String).
• The original source value is not changed as a result of a conversion.
18. Exceptions During Conversion
Because widening conversions always succeed, they do not
throw exceptions. Narrowing conversions, when they fail,
most commonly throw the following exceptions:
• InvalidCastException — if no conversion is defined
between the two types
• OverflowException — (integral types only) if the
converted value is too large for the target type