Posts Tagged ‘net programming’

Introduction to Visual Basic DotNet(VB.Net)

Tuesday, October 7th, 2008

Visual Basic.NET is modeled on the .NET framework. Therefore, along with the features of earlier versions of Visual Basic, Visual Basic.NET also inherits various features of the .NET framework. In this section, you will look at some of the new features in Visual Basic.NET that were unavailable in earlier versions of Visual Basic.Visual Basic.NET supports implementation inheritance in contrast to the earlier versions of Visual Basic that supported interface inheritance. In other words, with earlier versions of Visual Basic, you can only implement interfaces.

When you implement an interface in Visual Basic 6.0, you need to implement all the methods of the interface. Additionally, you need to rewrite the code each time you implement the interface. On the other hand, Visual Basic.NET supports implementation inheritance. This implies that, while creating applications in Visual Basic.NET, you can derive a class from another class, known as the base class. The derived class inherits all the methods and properties of the base class. In the derived class, you can either use the existing code of the base class or override the existing code. Therefore, with the help of implementation inheritance, code can be reused. Although a class in Visual Basic.NET can implement multiple interfaces, it can inherit from only one class.

Visual Basic.NET provides constructors and destructors. Constructors are used to initialize objects. In contrast, destructors are used to release the memory and resources used by destroyed objects. In Visual Basic.NET, the Sub New procedure replaces the Class_Initialize event. Unlike the Class_Initialize event available in earlier versions of Visual Basic, the Sub New procedure is executed when an object of the class is created. In addition, you cannot call the Sub New procedure. The Sub New procedure is the first procedure to be executed in a class. In Visual Basic.NET, the Sub Finalize procedure is available instead of the Class_Terminate event. The Sub Finalize procedure is used to complete the tasks that must be performed when an object is destroyed. The Sub Finalize procedure is called automatically when an object is destroyed.

Garbage collection is another new feature in Visual Basic.NET. The .NET framework monitors allocated resources such as objects and variables. In addition, the .NET framework automatically releases memory for reuse by destroying objects that are no longer in use. In Visual Basic 6.0, if you set an object to Nothing, the object is destroyed. In contrast, when an object is set to Nothing in Visual Basic.NET, it still occupies memory and uses other resources. However, the object is marked for garbage collection. Similarly, when an object is not referenced for a long period of time, it is marked for garbage collection. In Visual Basic.NET, the garbage collector checks for the objects that are not currently in use by applications. When the garbage collector comes across an object that is marked for garbage collection, it releases the memory occupied by the object. The garbage collector automatically handles the memory allocated to managed resources. However, you need to manage the memory allocated to unmanaged resources.

In the .NET framework, you can use the GC class, the Sub Finalize procedure, and the IDisposable interface to perform garbage collection operations for unmanaged resources. The GC class is present in the System namespace. It provides various methods that enable you to control the system garbage collector. The Sub Finalize procedure, which is a member of the Object class, acts as the destructor in the .NET framework. You can override the Sub Finalize procedure in your applications.
However, the Sub Finalize procedure is not executed when your application is executed. The GC class calls the Sub Finalize procedure to release memory occupied by a destroyed object. Thus, implementing the Sub Finalize procedure is an implicit way of managing resources. However, the .NET framework also provides an explicit way of managing resources in the form of the IDisposable interface. The IDisposable interface includes the Dispose method. After implementing the IDisposable interface, you can override the Dispose method in your applications. In the Dispose method, you can release resources and close database connections.Unlike earlier versions of Visual Basic, Visual Basic.NET supports overloading.
Overloading enables you to define multiple procedures with the same name, where each procedure has a different set of arguments. In addition to procedures, you can also use overloading for constructors and properties in a class. You need to use the Overloads keyword for overloading procedures. Consider a scenario in which you need to create a procedure that displays the address of an employee. You should be able to view the address of the employee based on either the employee name or the employee code. In such a situation, you can use an overloaded procedure. You will create two procedures.
Each procedure will have the same name but different arguments. The first procedure will take the employee name as the argument, and the second takes the employee code as the argument.The .NET framework class library is organized into namespaces. A namespace is a collection of classes. Namespaces are used to logically group classes within an assembly. These namespaces are available in all the .NET languages, including Visual Basic.NET.
In Visual Basic.NET, you must use the Imports statement to access the classes in namespaces.
For example, to use the button control defined in the System.Windows.Forms namespace, you must include the following statement at the beginning of your program:

Imports System.Windows.Forms

After adding the Imports statement, you can use the following code to create a new button:

Dim MyButton as Button

If you do not include the Imports statement in the program, however, you would need to use the full reference path of the class to create a button. If you didn’t include the Imports statement, you would use the following code to create a button:

Dim MyButton as System.Windows.Forms.Button

In addition to using the namespaces available in Visual Basic.NET, you can also create your own namespaces.Visual Basic.NET supports multithreading. An application that supports multithreading can handle multiple tasks simultaneously. You can use multithreading to decrease the time taken by an application to respond to user interaction. To do this, you must ensure that a separate thread in the application handles user interaction.

Visual Basic.NET supports structured exception handling, which enables you to detect and remove errors at runtime. In Visual Basic.NET, you need to use

Try…Catch…Finally statements to create exception handlers.
Using Try…Catch…Finally statements, you can create robust and effective exception handlers to improve the performance of your application.