A First Look At Vb_net

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View A First Look At Vb_net as PDF for free.

More details

  • Words: 1,580
  • Pages: 3
A First Look at VB.NET No doubt you've heard of Microsoft's .NET (pronounced 'dot net') by now. First off, please understand that .NET seems to be a buzzword over at Redmond right now so you'll see .NET appended to a lot of products and services. In this article, I'll be discussing the next generation of Visual Basic which is of course, VB.NET. I don't know the first thing about any of the other .NET initiatives since I have a very narrow focus on VB programming with and without AutoCAD. First the bad news: VB.NET doesn't resemble previous versions of Visual Basic. Now the good news: VB.NET doesn't resemble previous versions of Visual Basic. No, it's not doublespeak. Whether VB.NET represents the end of your growth with Visual Basic depends heavily on what you want from Visual Basic. If VB6 serves you well, it will continue to do so for years. However, if you're tired of having to look up API calls and dealing with Delphi zealots telling you that VB is a toy language, then VB.NET is the ticket for you. I've had a chance to work with the release candidate of Visual Studio.NET and I must confess that what I see is impressive. Here's a few highlights:

VS.NET is language independent Visual C++, C# (C-sharp), Visual J# and VB all operate on what's called the "Common Language Runtime" (CLR). As a consequence, the language you choose to write an application or components is largely irrelevant. In fact, you can use multiple languages to build different parts of the same application and they'll all play nicely together. You even use the same IDE for all the languages

VS.NET is platform dependent Currently, the CLR is for Windows only but there are several companies working on porting the CLR to other platforms, including Linux. Additionally, it should be noted that J# is not intended to create cross-platform apps. Instead, its goal is to allow you to use the Java language to create apps that will run on the CLR.

VB.NET does OOP for real One of the common gripes about Visual Basic is that it is not a true object oriented language. The accusation is true. VB classes offer code reuse and encapsulation but do not support implementation inheritance which is a great method for code reuse. Since OOP is the big change in VB.NET, this is where I will concentrate for the remainder of this article.

So what's the big deal? For starters, everything in VB.NET is a derived from Object. This means that everything is an object. For example, strings in VB are just an array of characters. In VB.NET, a String is an object, complete with methods and properties. Here's some code: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim s As New String("Object-oriented string") MsgBox(s.Length) End Sub As you can see, this is very different from the typical sub associated with a button click. For now, let's concentrate on the String object. Notice that I did not need to use VB's Len statement in order to determine the length of the string. Instead I asked the string itself for that information by using it's Length property.

Forget the Set A common problem for novice programmers is to forget when the Set keyword is required. With VB.NET, you can forget permanently. Since everything is an object, there is no need for VB's Set statement. In addition, you are now allowed to pass arguments when creating an object. This is called 'parameterized construction'.

Initializing objects 1 of 3

With VB, you must first create an object then use various methods to establish the initial state of the object. Failing to do so leads to buggy code since you can't be sure what state the object is in. With VB.NET you can create an object and initialize it in one step. When designing your own classes, you can even code several different versions of a constructor through the use of overloading. For each set of parameters you want to support during object creation, you simply create an additional Sub New.

Function overloading Overloading is a technique that allows you to create several functions that have the same name but take arguments that vary either in number or type. In VB, you can use optional arguments but if you fail to include a comma in place of a missing optional parameter, your code fails. As for varying types, you could always use variants in VB but they are the largest data type in OLE and therefore impose significant performance penalties when used in excess. So for the sake of efficiency, you might have two versions of a function: one that accepts Doubles and another that accepts Integers. Now you have to remember which one to use and when to use it. With overloading, you simply create two functions with the same name; one accepts Doubles and the other accepts Integers. VB.NET will call the correct version based upon the data types you supply when you call the function. This is the defining behavior of an OOP principle known as 'polymorphism'.

Implementation inheritance There are two types of inheritance: implementation inheritance and interface inheritance. VB6 supports the latter. Interface inheritance is a technique that guarantees objects will support a given set of services but does not dictate how those services are provided (for more on interface inheritance, please refer to the article "Interface Programming With Visual Basic"). The down side to interface inheritance is that you have to supply your own code for every interface method you implement, even if the implementation is exactly the same across all classes that implement the interface. Implementation inheritance comes into play when one class inherits another. For example, you may have an Employee class that supports basic properties like Name and SSN. After a bit, you may decide that you have two types of employees: hourly and salary. With VB, that's at least two classes and possibly one interface (to support common functionality). Using VB.NET, you can create two new classes (HourlyEmployee and SalariedEmployee) and have each one inherit the Employee class. Each new class automatically gains all the code you placed in their parent (or super) class. At this point, you are free to change how an existing method works or add new methods. Best of all, all three classes are still Employee classes so you can use a single Employee object to refer to any kind of Employee object.

Shared methods With VB6, using any method or property of class first required an instance of that class. With VB.NET, we now have the ability to support methods that can be called without an object. C++ calls this a static method. In VB.NET, it's called a shared method. An example of a shared method is the PI constant in the Math assembly. Whenever you need the value of PI, you just type Math.PI. No need for a Math object. This technique eliminates the need for global variables in order to share information between instances of a class.

Delegates Delegates are a new way of responding to messages. In the past, every button had it's own procedure. In order to run the same code when you press a different button, you had three choices: 1. 2. 3.

Copy and paste your code Create a control array and use the Index argument to determine which button was pressed Write a separate routine and have each button pass itself (or some other identifying characteristic) to that routine

In VB.NET, you can use either the Handles keyword or the AddHandler method to associate a method with a given event. Below is an example of each technique: Private Sub ShowName(ByVal sender As System.Object, _ ByVal e As System.EventArgs) If TypeOf sender Is Button Then Dim btn As New Button() 2 of 3

btn = sender MsgBox(btn.Text) End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load AddHandler Button1.Click, AddressOf ShowName AddHandler Button1.Click, AddressOf ShowName End Sub Using AddHandler

Private Sub ShowName(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click, Button2.Click If TypeOf sender Is Button Then Dim btn As New Button() btn = sender MsgBox(btn.Text) End If End Sub Using the Handles keyword While both methods display the name of the button clicked, the AddHandler method has the added advantage of allowing you to stop processing a given event through the use of the RemoveHandler method.

There's more but... What I've covered here is just the tip of the iceberg. Unfortunately, I have not yet had enough time to dig more deeply into the language. As you can see, VB.NET is definitely not your daddy's VB. It requires a deeper understanding of formal programming concepts and represents a radical departure from the ease-of-use VB programmers have grown accustomed to. In exchange for the added complexity, VB.NET allows VB programmers to create a wide variety of applications and places very few restrictions on what you can do. Keep in mind that Visual Studio 6 and Visual Studio.NET will peacefully coexist on the same machine so there's no reason for you to choose one over the other. In fact, due to the numerous differences between the languages, porting anything but the most trivial application to VB.NET would benefit from a complete rewrite; so you might consider keeping VB6 around to maintain your existing apps and using VB.NET for new projects.

3 of 3

Related Documents

2001 - A First Look
May 2020 8
A Look At Prophesy
November 2019 55
A Look At Canada
May 2020 31
A Look At Tcl
August 2019 53