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
Add a Comment