devxlogo

Visual Basic.NET, Part I: Class Structures and Interfaces

Visual Basic.NET, Part I: Class Structures and Interfaces

his is the first of a series of articles on Visual Basic 7.0; now known as Visual Basic.NET. It is well known that VB in its new reincarnation is undergoing revolutionary changes that will make it a sophisticated object oriented language. There is a lot of ‘big picture’ information available on VB.NET and the .NET platform. This series of articles will go beyond the generalities and previews to provide specifics and hands-on code so that you can familiarize yourself with the concepts and see how they are implemented. Note that these articles are based on a beta version of Visual Studio.NET that is subject to change. You can download a beta version of Visual Studio.NET at msdn.microsoft.com/downloads. In this article we will discuss how to define and implement classes and interfaces in VB.NET.

Class Structure
Currently in VB, classes are created in individual files. When you add a class to a project VB adds a corresponding file to the project. In VB.NET classes are not dependent on files, rather they are declared in Class statement blocks. Multiple classes can be declared within one file. Here are two classes that may be defined in one file:

Class Order        Public ID As Long        Public ShippingAddress As String        Function ExecuteOrder(Value as Integer) As Boolean		'code for function goes here        End FunctionEnd ClassClass OrderItem	Public OrderID as Long	Public ItemID as Long	Sub UpdateItem()		'code for sub goes hereEnd SubEnd Class

As you can see from the sample code, all variables and procedures are declared and implemented within the class statement.A class can also contain other classes, which is known as a nested class:

Class Order        Public ID As Long        Public ShippingAddress As String        Function ExecuteOrder(Value as Integer) As Boolean		'code for function goes here        End Function	Class OrderItem		Public OrderID as Long		Public ItemID as Long		Sub UpdateItem()			'code for sub goes hereEnd SubEnd ClassEnd Class

Class Constructor
All classes in VB.NET have a sub procedure called New. It is run when a class is initially created and is known as a Constructor. It replaces VB’s Class_Initialize procedure. The New constructor, unlike Class_Initialize, can only run once and cannot be called a second time after the class has been initialized. A major enhancement to the VB.NET constructor is that it can take parameters. Currently in VB, if you want a client to initialize a class with a certain state the client has to set the state after the class has initialized. In VB.NET a client can set state while instantiating the class. Here is how it works. By default all classes have an implicit New procedure (it is not shown in the code window) that is automatically called by the Common Language Runtime (CLR). However, you can override the default New procedure by adding a sub procedure called New (Parameter List) to the class definition. You can then add code statements to the body of the procedure and/or add parameters:

Class OrderPublic ID As LongPublic ShippingAddress As StringSub New (OrderID as Long)	mybase.NewID=OrderIDEnd SubFunction ExecuteOrder(Value as Integer) As Boolean		'code for function goes here     End FunctionEnd Class

This class would be instantiated with the following code:

Dim objOrder as OrderobjOrder= New Order(4323)

Parameterized constructors allow to you to ensure that class instances have required state upon initialization. Currently, a client has to set state after a class has been initialized and the procedures in the class have to ensure that the state has been set before running any code dependent on the state.

Note: The first statement in a constructor must be mybase.New. The reason for this is that all classes inherit from Object class and a constructor must call its base class’ constructor.

Interfaces
An interface is basically a class definition. It itself cannot be instantiated, rather it is implemented by other classes. Interfaces can be defined and implemented in VB6 albeit with some workarounds and limitations. For example, in VB6 you cannot use one variable alone to access the methods of the interface and of the implementing class?you have to declare two variables, one as the interface and the other as the implementing class and point both of them to the same instance. VB.NET has a different and more straightforward implementation of interfaces. Like classes, interfaces can be declared in any class file and are declared with a Interface End Interface statement block:

Interface IAudit	Public orderID as long	Public Function Log(Msg as String)End Interface

A class implements an interface using the Implements keyword as in VB6. However, unlike VB6 the interface’s procedures are not added to the class. Instead, you have to manually link procedures of the class to the interface procedures using the Implements keyword at the end of the procedure declaration. The class procedure and the interface procedure do not have to have the same name:

Interface IAudit	Public orderID as long	Public Function Log(Msg as String)End InterfaceClass Order	Implements IAuditPublic ID As LongPublic ShippingAddress As StringSub New (OrderID as Long)ID=OrderIDEnd SubFunction ExecuteOrder(Value as Integer) As Boolean		'code for function goes here     End Function		Function Save(Msg as String) Implements IAudit.Log		End FunctionEnd Class

A class can implement multiple interfaces and one class procedure can be used to implement multiple procedures from multiple interfaces:

Interface IAudit	Public orderID as long	Public Function Log(Msg as String)End Interface	Interface IData		Function SaveData()	End InterfaceClass Order	Implements IAudit	Implements IDataPublic ID As LongPublic ShippingAddress As StringSub New (OrderID as Long)ID=OrderIDEnd SubFunction ExecuteOrder(Value as Integer) As Boolean		'code for function goes here     End Function		Function Save(Msg as String) Implements IAudit.Log, IData.SaveData		'code for function goes hereEnd FunctionEnd Class

A client can access the methods of the interface through a variable declared as implementing class’ type:

Dim objOrder as Order()objOrder=New Order(234)objOrder.Log

The call to the Log method of the IAudit interface will run the Save method of the order class.

Conclusion
As you can see, VB is growing in sophistication and will be a big change for VB developers. I hope these articles will help you get prepared for the change. The next article will cover VB.NET’s implementation of inheritance.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist