Browse DevX
Sign up for e-mail newsletters from DevX


Visual Basic .NET: A Punch of a Tool : Page 4

The newest version of Visual Basic now has support for full object-oriented programming, provides access to the .NET Framework and uses the power and flexibility of the Common Language Runtime. Never have there been more reasons for VB developers to consider making the move to Visual Basic .NET. Yet, amidst the excitement surrounding the .NET platform, some major productivity features have been lost in the shuffle.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Pretty-Lister
As mentioned above, the background compiler supplies rich contextual information to several components of the editor, one of which is the Auto-Formatter/Pretty-Lister. The Pretty-Lister is responsible for making sure identifiers have consistent case (VB's case-insensitivity is a huge productivity boost; there, it's said) and that appropriate white space gets inserted between operators and keywords.

The Pretty-Lister's job also includes consistent code formatting. For those of us in large teams who struggle with coding-style conventions and documents, the automatic code-formatting feature of VB.NET is a dream come true. Based on the current context, the editor indents bodies of code to the appropriate nesting level. Programmers no longer have to break concentration and spend precious brain cycles worrying about the TAB key. Auto-formatting also makes it really easy to spot errors with End constructs such as "End Sub" and "End If" because it becomes completely obvious when there are too many or too few.

The importance of consistent formatting cannot be overestimated. Consistent formatting helps increase code maintainability; this is why code-style documents exist. Any help the code editor gives to the programmer in this regard directly impacts productivity because it lets the programmer concentrate on more important things, like architecture and algorithms.

Declarative Event Hookup using WithEvents
The .NET Event model is built upon delegates, which are basically type-safe function pointers. Raising an event is simply invoking one of these delegates. The difficulty is that the event must be "hooked-up," that is, the object sourcing the event must be made aware at runtime of the delegate it needs to invoke when an event gets raised. The AddHandler and RemoveHandler statements, used for dynamically hooking-up events at runtime, take care of all these necessary details:

Class EventSource Event Click() ... End Class Class Driver Shared Sub Main() Dim x As New EventSource() AddHandler x.Click, AddressOf OnClick ... End Sub Sub OnClick() Console.WriteLine("Click Event raised") End Sub End Class

This is the extent of event handling support in other languages. You must manually hook up and unhook events with statements located somewhere in a method or constructor. This keeps you from quickly determining event handling behavior due to the fact that the events in which a class handles can vary at runtime. This can produce subtle and hard-to-detect bugs.

VB.NET provides another way to hook up events: declarative event handling, that makes event handling part of the class definition.
VB.NET provides another, faster, way to hook up events with a technique called declarative event handling. Declarative event handling makes event handling part of the class definition. In many ways, this is more convenient because event handling can be built into your component architecture and results in more manageable code.

Declaratively handling events is a two-step process in VB.NET. First, the object that is going to raise events must be stored in a field with the WithEvents modifier. This indicates the field's availability for declarative event hookup. Then, methods can state they handle a particular event raised by the object by specifying a Handles clause in the method declaration. For example:

Class Driver WithEvents x As New EventSource() Sub OnClick() Handles x.Click Console.WriteLine("Click Event raised") End Sub End Class

In this example, OnClick visibly handles the Click event raised by the object stored in field x. This makes it easy for you to quickly analyze the Driver class and recognize its event handling characteristics. Furthermore, multiple events can be handled with the same handler by specifying additional events in the Handles clause:

Sub OnClickAndKey() Handles x.Click, x.KeyPress Console.WriteLine("events raised") End Sub

See how much easier this is than the AddHandler and AddressOf approach? This is an example of the gestalt of VB.NET—an easy approach to handling the 80% case, with the capability of handling the 20% case as well.

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