Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

In this article we will discuss how to define and implement classes and interfaces in VB.NET.


advertisement
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 Function End Class Class OrderItem Public OrderID as Long Public ItemID as Long Sub UpdateItem() 'code for sub goes here End Sub End 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 here End Sub End Class End 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 Order Public ID As Long Public ShippingAddress As String Sub New (OrderID as Long) mybase.New ID=OrderID End Sub Function ExecuteOrder(Value as Integer) As Boolean 'code for function goes here End Function End Class

This class would be instantiated with the following code:

Dim objOrder as Order objOrder= 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 Interface Class Order Implements IAudit Public ID As Long Public ShippingAddress As String Sub New (OrderID as Long) ID=OrderID End Sub Function ExecuteOrder(Value as Integer) As Boolean 'code for function goes here End Function Function Save(Msg as String) Implements IAudit.Log End Function End 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 Interface Class Order Implements IAudit Implements IData Public ID As Long Public ShippingAddress As String Sub New (OrderID as Long) ID=OrderID End Sub Function 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 here End Function End 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.



   
Michael Gellis works at Flash Creative Management (a Microsoft Partner) as a Visual Basic developer. He is a Microsoft Certified Professional in VB5 and VB6 and is co-writing a book for Sybex on VB 6.0 and distributed applications. You can reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date