a first look at vb_net

Upload: jalilalwi

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 A First Look at VB_net

    1/3

    A First Look at VB.NETNo doubt you've heard of Microsoft's .NET (pronounced 'dot net') by now. First off, please understand that .NET seems to be a buzzwordover at Redmond right now so you'll see .NET appended to a lot of products and services.

    n 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 theother .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 fromVisual 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 dealingwith Delphi zealots telling you that VB is a toy language, then VB.NET is the ticket for you.

    '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 fewhighlights:

    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, theanguage 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 languageo 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 codeeuse and encapsulation but do not support implementation inheritance which is a great method for code reuse. Since OOP is the big changen 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 anarray of characters. In VB.NET, a String is an object, complete with methods and properties. Here's some code:

    rivate 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 thatnformation 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. Sinceeverything 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

  • 8/14/2019 A First Look at VB_net

    2/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 buggycode 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 designingyour 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 significantperformance 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. Nowyou have to remember which one to use and when to use it. With overloading, you simply create two functions with the same name; oneaccepts Doubles and the other accepts Integers. VB.NET will call the correct version based upon the data types you supply when you call thefunction.

    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 aechnique that guarantees objects will support a given set of services but does not dictate how those services are provided (for more onnterface 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 themplementation is exactly the same across all classes that implement the interface.

    mplementation inheritance comes into play when one class inherits another. For example, you may have an Employee class that supportsbasic 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 ateast 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 anexisting method works or add new methods. Best of all, all three classes are still Employee classes so you can use a single Employee objecto 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 supportmethods 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 needfor 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 youpress a different button, you had three choices:

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

    n VB.NET, you can use either the Handles keyword or the AddHandler method to associate a method with a given event. Below is anexample of each technique:

    rivate Sub ShowName(ByVal sender As System.Object, _ByVal e As System.EventArgs)

    If TypeOf sender Is Button ThenDim btn As New Button()

    2 of 3

    http://www.acadx.com/articles/006.htmhttp://www.acadx.com/articles/006.htm
  • 8/14/2019 A First Look at VB_net

    3/3

    btn = senderMsgBox(btn.Text)

    End IfEnd Sub

    rivate Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles MyBase.Load

    AddHandler Button1.Click, AddressOf ShowNameAddHandler Button1.Click, AddressOf ShowName

    End Sub

    Using AddHandler

    rivate Sub ShowName(ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles Button1.Click, Button2.Click

    If TypeOf sender Is Button ThenDim btn As New Button()btn = senderMsgBox(btn.Text)

    End IfEnd 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 stopprocessing 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. Asyou can see, VB.NET is definitely not your daddy's VB. It requires a deeper understanding of formal programming concepts and represents aadical departure from the ease-of-use VB programmers have grown accustomed to.

    n exchange for the added complexity, VB.NET allows VB programmers to create a wide variety of applications and places very fewestrictions on what you can do. Keep in mind that Visual Studio 6 and Visual Studio.NET will peacefully coexist on the same machine sohere's no reason for you to choose one over the other. In fact, due to the numerous differences between the languages, porting anything buthe 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