CAT | Vb.net
The Option Statement
There is one statement that is very important to know when making an application- Option statement
There are three option statements, these are as follows:
(a) Option Explicit:
It has two modes:
1. On
2. Off
Any VB.net program has ‘On’ mode of option explicit by default. If program has ‘Option explicit On’ statement than it requires all variables have proper deceleration otherwise it gives compile time error and another mode is ‘Off’, if we use ‘Option explicit Off’ statement than vb.net create variable declaration automatically and program does not give an error
We can take an example for better understanding:
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TempString = “Hello”
MessageBox.Show(TempString)
End Sub
End Class
Above program gives an error ‘Name TempString is not declared’
Option Explicit Off
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TempString = “Hello”
MessageBox.Show(TempString)
End Sub
End Class
Above program run continue without error
Note: For better coding it is recommended to declare variables with Dim keyword and data type.
(b) Option Compare:
This statement also has two modes
1. Binary
2. Text
Option Compare is Binary by default; we can change string comparison method by set the text or Binary see example:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If “Hello” = “HELLO” Then
MessageBox.Show(“True”)
Else
MessageBox.Show(“False”)
End If
End Sub
End Class
In above program if we use Option Compare Binary,messagebox show ‘false’ and if we use ‘Text’ mode than messagebox show ‘True. that means when we set Option Compare to Text we can able compare string with Case insensitive comparision.
(c) Option Strict:
It has also two modes:
1. On
2. Off
Option Strict Off is the default mode. When you assign a value of one type to a variable of another type ,Visual Basic will consider that an error if this option is on and there is any possibility of data loss.
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tempInt As Integer = 2010
MessageBox.Show(tempInt)
End Sub
End Class
Value type:
Value types are primitive types that are mapped directly to the FCL. Like Int32 maps to System.Int32, double maps to System.double. All value types are stored on stack. All the value types are derived from System.ValueType. All structures and enumerated types that are derived from System.ValueType are created on stack, hence known as ValueType.
An object of a value type stores its associated data directly within itself. Any changes to that data do not affect any other object. For example, the predefined arithmetic types, such as int and double, are value types.
When we write:
Dim pi As Double = 3.14159
The value 3.14159 is directly stored within pi.
When we initialize or assign one value type with another, the data contained in the one is copied to the second. The two objects remain independent.
For example, when we write
Dim shortPi As Double = pi
although both pi and shortPi now hold the same value, the values are distinct instances contained in independent objects. We call this a deep copy. If we change the value stored by shortPi, shortPi = 3.14; the value of pi remains unchanged.
Reference type:
Reference Types are different from value types in such a way that memory is allocated to them from the heap. All the classes are of reference type. VB new operator returns the memory address of the object. Reference types are stored on the run-time heap; they may only be accessed through a reference to that storage. This allows the garbage collector to track outstanding references to a particular instance and free the instance when no references remain. A variable of reference type always contains a reference to a value of that type or a null reference. A null reference refers to nothing; it is invalid to do anything with a null reference except assign it. Assignment to a variable of a reference type creates a copy of the reference, not a copy of the value being referenced.
- The Object Type
- The class Type
- Interfaces
- Delegates
- The string type
- Arrays
Dim obj1 As Myobj ‘ obj1 is a reference type assuming Myobj is of class type.
obj1 = New myobj()
obj1 is reference type variable (assuming Myobj is a class type variable).compiler allocates memory for this object on the heap and its address is stored in obj1.
Boxing and Unboxing in VB.Net:
VB provides us with Value types and Reference Types. Value Types are stored on the stack and Reference types are stored on the heap. The conversion of value type to reference type is known as Boxing and converting reference type back to the value type is known as Unboxing.
Boxing:
Convert ValueTypes to Reference Types also known as boxing.
Dim x As Int32 = 10
Dim o As Object = x ‘ Implicit boxing
Console.WriteLine(“The Object o = {0}”, o) ‘ prints out 10
Dim x As Int32 = 10
Dim o As Object = CObj(x) ‘ Explicit Boxing
Console.WriteLine(“The object o = {0}”, o) ‘ prints out 10
Unboxing:
UnBoxing an object type back to value type.
Dim x As Int32 = 5
Dim o As Object = x ‘ Implicit Boxing
x = o ‘ Implicit UnBoxing
Dim x As Int32 = 5
Dim o As Object = x ‘ Implicit Boxing
x = CInt(Fix(o)) ‘ Explicit UnBoxing
Introduction
Before the .NET platform was introduced, we had to deal with the predecessors of assemblies: normal DLLs exporting global functions and COM DLLs exporting COM classes. Microsoft itself introduced the phrase DLL Hell to describe traditional problems with DLLs.
The answer to DLL Hell
The .NET platform answer to DLL Hell and to all of its problems is Assemblies.
Assemblies are self describing installation units, consisting of one or more files.
Features of assemblies
(1) Assemblies are self-describing.
(2) Version dependencies are recorded inside an assembly manifest.
(3) Assemblies can be loaded side-by-side.
(4) Application isolation is endured using application domains.
(5) Installation can be as easy as copying the files that belong to an assembly.
(6) Assemblies can be private or shared.
Assembly Structure
| Assembly Metadata |
| Type Metadata |
| IL Code |
|
Resources |
In this example, the assembly metadata, type metadata, MSIL code, and resources all in one file- Component.dll. The assembly consists of a single file.
Assembly Manifests
An important part of an assembly is a manifest, which is part of the metadata. It describes the assembly with all the information that needed to reference it and lists all its dependencies.
Private and Shared Assemblies
A private assembly is found either in the same directory as the application, or within one of its subdirectories. With the private assembly it is not necessary to think about naming conflicts with other classes or versioning problems. Private assemblies are the normal way to build assemblies, especially when applications and components are built within the same company.
While using shared assemblies, we have to aware of some rules. The assembly must be unique and therefore must also have a unique name called a strong name. Shared assemblies is stored in GAC (Global Assembly cache).
It provides a number of services, including the following:
- Code management (loading and execution)
- Common type System
- Conversion of IL to native code
- Access to metadata (enhanced type information)
- Managing memory for managed objects
- Enforcement of code access security
- Exception handling, including cross-language exceptions
- Interoperation between managed code, COM objects, and pre-existing DLLs (unmanaged code and data)
- Support for developer services (profiling, debugging, and so on)
1. CLR manages memory, thread execution, code execution, compilation code safety verification and other system services.
2. For security reasons, managed code is assigned varying degrees of trust based on origin. This prevents or allows the managed component from performing file access operations, registry access operations or other sensitive functions even within the same active application.
3. The Runtime enforces code robustness by implementing strict type and code verification infrastructure called Common type System (CTS). The CTS ensures that all managed code is self describing and all Microsoft or third party language compiler generated codes conform to CTS. This enables the managed code to consume other managed types and enforce strict type fidelity and type safety.
4. CLR eliminates many common software issues like handling of object layout, references to objects and garbage clearance. This type of memory management prevents memory leaks and invalid memory references.
5. The CLR also accelerates developer productivity. The programmer is free to choose the language of the application without worrying about compatibility and integration issues. He is also enabled to take advantage of the runtime and the class library of the .NET Framework and also harvest components from other applications written in different languages by different developers. This implicitly eases the process of migration.
6. Though CLR aims to be futuristic software, it lends support to existing applications. The interoperability between the managed and unmanaged codes makes this process extremely simple.
7. The design of the CLR is geared towards enhancing performance. The Just-in-time (JIT) compiling enables managed code to run in the native machine language of the system executing it. During the process the memory manager removes the possibilities of fragmented memory and increases memory locality-of-reference to enhance performance.
8. Finally, server side applications can host runtime. High performance servers like Microsoft SQL Server and Internet Information Services can host this CLR and the infrastructure so provided can be used to write business logic while enjoying the best benefits of enterprise server support.
The Class Library is an object oriented collection of reusable types. It is comprehensive and the types can be used to develop command line applications or GUI applications such as Web forms or XML Web services. Unmanaged components that load CLR into their processes can be hosted by the .NET Framework to initiate the execution of managed code. This creates a software environment that exploits both the managed and unmanaged codes. The.NET Framework also provides a number of runtime hosts and supports third party runtime hosts
Common Type System
Common Type System is the part of .NET Framework built in with CLR which is responsible for defining, managing different language’s operation supported by .NET Framework.
The Common Type System (CTS) is a standard that specifies how Type definitions and specific values of Types are represented in computer memory. It is intended to allow programs written in different programming languages to easily share information. As used in programming languages, a Type can be described as a definition of a set of values (for example, “all integers between 0 and 10″), and the allowable operations on those values (for example, addition and subtraction).
The CTS provides every language running on the .NET platform with a base set of data types. A language that makes available a limited number of types or that limits the programmer’s ability to extend the language’s built-in types isn’t a language with a long life expectancy. However, having a unified type system has many other benefits as well.
- Every thing in the CTS is an object.
- The CTS is responsible for defining the types that can be used across the .NET languages.
- All objects implicitly derive from a single base class defined as a part of the CTS. This base class is called system.object.
- The CTS types are value types and reference types.
The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class libraries, and a componentized version of Active Server Pages called ASP.NET.
The Microsoft .NET Framework is a managed code programming model for building applications on Windows clients, servers, and mobile or embedded devices. Developers use .NET to build applications of many types: Web applications, server applications, smart client applications, console applications, database applications, and more.
the overall Architecture of .NET Framework
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)

