/  22
 
Integer Types in VB.NET...Integer 
types in VB.Net are used to represent whole numbers. Depending on the amount of memory required to store an integer the data type in VB.net is categorized as,
Byte
,
Short
,
Integer 
and
Long
. The following table lists the data type and its size.DataTypeSize in BytesByte 1Short 2Integer 4Long 8Following statement shows how we can declare an integer variable in VB.NET.Dim x, y As Integer Here
x
and
y
are two integer variables and as they are not initialised with any number, they wouldbe initialised to 0.Dim i As Short = 12The variable
i
declared in this statement would hold value 12.VB.NET provides logical operators,
And
and
AndAlso
that can be used to help evaluateexpressions. Consider following statement,If age < 10 And number > 4 Then// codeEnd If Here we want that if the value of 
age
is greater than 10, then the second condition should not beevaluated. Since the conditions given above are connected using
And
, both the conditions wouldget evaluated. In other words, even if the first condition evaluates to false,
And
evaluates thesecond condition. However, in case of 
AndAlso
operator, it evaluates the second condition only if the first condition evaluates to true. For example,If age < 10 AndAlso number > 4 Then// codeEnd If If the value of 
age
is greater than 10, the evaluation of the expression stops and the operator would return false. Thus, using
AndAlso
helps evaluating expressions efficiently.
There are two types of procedures in VB.NET-
Functions
and
Subroutines
. The basicdifference between the two is that a subroutine does not return a value whereas a functionreturns a value. Function is a collection of logical statements which perform a specifictask and return some value. Hence whenever you want to return something to a callingfunction use a function and to display something use a subroutine. Thus, for displaying a
 
menu we would use a subroutine and for calculating a factorial of a number and returningit to the calling function we would use a function.
Constructor in VB .NET
In VB.NET the constructor name is not same as the name of the class. A keyword
New
is used todefine a constructor. Following code snippet shows how to declare a constructor:Class sampleDim i As Integer Sub New( )i = 5End SubEnd Class Module Module1Sub Main( )Dim obj As sampleobj = New sample( )End SubEnd ModuleIn the above example in the class
sample
we have defined a constructor to initialise the datamember 
i
. In
main( )
we have created an object
obj
of the class
sample
. As soon as the objectgets created the constructor gets called and the value of 
i
gets intialised to 5.We can use the
MinValue
and
MaxValue
properties to get the minimum as well as maximumrange of each data type. This is shown in following example.Module RangesSub Main( )Console.WriteLine("Byte Range: {0} to {1}", Byte.MinValue, Byte.MaxValue)Console.WriteLine("Short Range: {0} to {1}", Short.MinValue, Short.MaxValue)Console.WriteLine("Integer Range: {0} to {1}", Integer.MinValue, Integer.MaxValue)Console.WriteLine("Long Range: {0} to {1}", Long.MinValue, Long.MaxValue)End SubEnd Module
Namespaces...
Much of interesting functionality available in VB .NET is provided by the .NET Framework ClassLibrary. Namespaces are used to hold collection of related classes. Console I/O is provided bythe .NET
System.Console
class. The
WriteLine( )
method is used to display a line of text.System.Console.WriteLine ( "Hello World" )Reference to
System.Console.WriteLine
may be shortened to
Console.WriteLine
if you importthe
System
namespace.Import System...Console.WriteLine ( "Hello World" )
 
The
Import
statement tells compiler to look in the
System
namespace before generating an error whenever an unknown class or function is referenced.
Value Types Vs Reference Types
In VB.NET variables are either value types or of reference types. The difference betweenthe value type and reference type is that the data of value type gets stored on the stack and that of the reference type gets stored on the heap. The variables of the value typesdirectly contain the data, whereas, reference types contain references (or addresses) of thedata. Examples of value types are the primitive data types integer, floating points,structures whereas examples of reference types are arrays and classes
Arrays...
We can declare an array as shown below:Dim arr( ) As Integer arr = New Integer ( 5 ) { }Here, the first statement creates only a reference to one dimensional array. By default
arr 
holdsnothing. The second statement allocates memory for 5 integers. The space for 
arr 
is allocated onthe stack. Space of array elements gets allocated on heap. The elements of 
arr 
would be 0 as bydefault array elements are initialised to zero. One more way of declaring an array is given below:Dim arr( ) As Integer = { 1, 2 , 3 ,4 }Here again
arr 
is created on the stack and the actual array elements are created on the heap.These array elements are initialised with values 1, 2, 3, 4 respectively. Note that when weinitialize an array at the same place where we are declaring it then there is no need of 
New
statement.In .NET arrays are implemented as objects. An array gets automatically derived from
System.Array
class. This is because when we create an array a class gets created from the
System.Array
class. This class internally maintains an array. An object of this class is alsocreated to which the array reference points. Hence using the reference we can call methods andaccess properties of the
System.Array
class. In VB.NET the lower bound of an array is alwayszero. Thus with the statement
Dim arr(3) As Integer 
an array of 4 elements get created.VB.NET provides the flexibility to use
Value types
as
Reference types
, as and when required.Whenever a value type is converted into a reference type it is known as
Boxing
. On the other hand when a reference type is converted into value type it is known as
Unboxing
. Both the
WriteLine( )
and the
ReadLine( )
methods use this concept of 
Boxing
and
Unboxing
respectively.For example, consider following statement:WriteLine ( Dim s As string, ParamArray arr( ) As Object )Here, all the objects except the first are collected in the
ParamArray
of type
Object
. Hence thereis no restriction on the number of parameters being printed in the list. In the
WriteLine( )
methodif we pass an integer variable, the compiler implicitly converts an integer variable ( value type )into an object ( reference type ) using boxing and collects it as the first element of the

Share & Embed

More from this user

Add a Comment

Characters: ...