Developer

OOP designs with VB.NET and Java

As a .NET developer, it might surprise you what you can learn from the Java crowd. The two languages may be more similar than you think.


The more I work with Visual Basic .NET, the more I appreciate the design of the language and structure of .NET assemblies. Being a VB programmer, I was happy to learn that my experience writing Java and J2EE applications was extremely useful when switching to .NET. The more both languages mature, the more they resemble each other. Even though Java is the de facto OOP (object-oriented programming) language, much of its design has been influenced by C++ and Smalltalk.

VB.NET has adopted several functions and traits of other OOP languages, including Java. Visual Basic .NET has turned primitive types (strings, integers, doubles, etc.) into objects with properties and methods; it has introduced Java-style error handling (with Try and Catch); it compiles code into Microsoft Intermediate Language (IL); and it offers XML-based application configurations. More important is the ability to design object-oriented solutions that can be implemented in any language. In this article, I’m comparing the Visual Basic .NET language to Java, but VB.NET now essentially stands on its own, comparable to other OOP languages.

On the other side of the fence, Sun recently announced it will be introducing a .NET-style development environment in the summer of 2003. Whether the new environment will bring Java into the mainstream remains to be seen. One of the benefits of Java not having such a friendly user interface was that programmers usually had to spend more time learning the fundamentals of Java programming and OOP design before writing an application. With Visual Basic, developers can write a program any way they want (quite often the wrong way), which is a major problem when supporting an application. When Sun’s new IDE for Java appears this summer, Sun should include some basic tutorials to give new Java programmers a taste of the importance of OOP design.

Is OOP syntax independent?
In the Visual Basic .NET language, as in Java, everything is an object—strings, integers, and other primitive types included. Previous versions of the Visual Basic language included many built-in functions to handle strings; a string or integer did not have properties and methods. Now, in Visual Basic .NET, strings and Java strings are almost the same. The same is true for primitive types such as integers and doubles. In Java, int and double (the primitives) still exist, but in Visual Basic .NET, they have been successfully replaced by their respective objects.

Converting strings and numbers to objects allows for cleaner code and less reliance on the programming language itself. As an example, to get a character from a string in earlier versions of Visual Basic, a programmer might write:
 
sMyString = Mid(sMyString,3,4)
 

Now, this can be replaced by:
 
sMyString = sMyString.substring(3,4)
 

The VB commands are still available and sometimes still needed if conversion becomes a problem, but in the second part of the example above, the syntax is the same for VB, Java, and C#. In my experience, VB programmers are having few problems forsaking the old VB syntax for the “new” OOP techniques.

Event handlers are another, more powerful addition to Visual Basic .NET that can likewise be compared to Java. (Event handlers are also called “listeners” in Java.) Using event handlers, a programmer can assign default methods to objects. For example, a developer working in VB.NET can add a handler to the Datagrid for processing changes to the data. These event handlers work similarly to the way callback functions have worked in the previous two versions of Visual Basic.

Working through design issues
In OOP the “has a” and “is a” statements are used to relate objects to one another. A good example of this type of OOP relationship with VB.NET is the way default and cancel buttons are implemented on forms. In VB 6.0, the programmer could set the default value on a button so [Enter] would act as that button. I was very puzzled when I saw this property missing in VB.NET. In actuality, the functionality is still there, but it has been moved to the form itself. Now every form “has a” default button property, whereby a button object can be assigned to this property of the form. This change makes a lot of sense, since a form can have only one button that acts as a default and one that acts as a cancel button.

One of my main concerns in moving to a true OOP environment with Visual Basic .NET is that now there are more design issues. As with Java, using OOP requires a lot more time up front to design all the classes and their relationships. If this time isn’t invested up front, programmers on the team need to be flexible and understand the importance of reworking the design as the program or project progresses.

In Visual Basic 6, many programmers still tend to write large chunks of procedural code. Even though this code can be ported over to Visual Basic .NET, VB programmers need to incorporate OOP practices and design the solution around objects and patterns before writing the code to solve the task at hand.

Configuring enterprise applications
Visual Studio has begun implementing some J2EE-like techniques to configure applications and solutions, primarily through XML configuration files. The app.config file gives an organization a standard means to set application configurations. .NET security can be configured through the control panel. The closest Java example of this type of configuration is the security.properties file.

Web applications also now contain a webconfig.xml file. With this file, programmers can select and configure database connection strings, security, and other options. This type of configuration makes it easier for Java programmers to understand how .NET is working, but more importantly, it gives Microsoft developers a standard way to configure and deploy applications.

More options for developers
It’s evident that Java and Visual Basic are moving closer together. As OOP matures, I’m sure we will see languages such as these become more and more similar. For instance, C# is closer to Java than it is to Visual Basic .NET.

Getting a good design for an application is always a challenge. Visual Basic .NET has made this challenge easier. Now that VB is an object-oriented programming language, a lot of design and programming options have opened up for developers. Likewise, architects can achieve OOP objectives (such as reusable classes) much more easily. The challenge will be instilling OOP design into programmers’ practices and convincing management to increase the time allotted to architect the best solution.

 

Editor's Picks