How Your Code Changes

In just a few pages, we ve looked at exactly what .NET is and figured out how we can quickly create both a Windows application and semi-interactive Web site. But these aren t the only areas to see change. The Visual Basic language itself has shifted, and this section provides an overview of the main differences.

Understanding the Framework

The big change for most developers is figuring out that thing called the .NET Framework. Well, you can imagine it as a very clever, very big runtime for your applications. It holds a mass of classes that allow you to do everything from plugging into databases to drawing graphics on the screen. An easy-to-use Windows API, if you will.

All those classes in the .NET Framework are available to any .NET language, meaning that, no matter whether you re a C# or VB .NET programmer, it s a level playing field.

Because there are literally thousands of these classes, they re split into namespaces to help organize everything. The master .NET namespace is called System . Filed under this System namespace is, for example, the Math class, which includes a shared function called Round . I can write code to access this function as so:

 Dim dblResult As Double  dblResult = System.Math.Round(3.142, 1) 

So, just by typing the namespace location, I ve tapped into the .NET Framework to round my number for me.


Shared is a new keyword in VB .NET. It refers to a procedure in a class that doesn t require a new instance of that class to run it.

If you re a die-hard fan of Visual Basic 6, you ll find equivalents for all the old VB functions in the Microsoft.VisualBasic namespace. But, where possible, you re best going with the new .NET Framework way of doing things. If you don t, well, it s just not cricket.

It s not just your code that needs the .NET Framework to run: your actual applications do, too. If you remember, earlier in this chapter we saw our Windows form tapping into the .NET Framework, by inheriting from System.Windows.Forms.Form , the Form class in the System.Windows.Forms namespace. And later, our Web application inherited from System.Web.UI.Page .


To save yourself having to type out full namespaces every time, you can use the Imports keyword to add a default reference to that particular namespace. You can also do this through the Project Properties dialog box.

Integer Upgrades

Visual Basic has changed the way in which it stores integers to bring it in line with other languages. Thankfully, they ve all been upgraded, meaning they now hold greater- ranging values, so you can still get away with doing it the old way. However, just in case you re conscious of wasting those extra bytes of memory, Table 1-1 shows how it s altered .

Table 1-1. Integer Changes






8-bit integer



16-bit integer



32-bit integer



64-bit integer

Also, if you re accustomed to converting your numeric data types to use the Windows API, stop ! VB .NET is now on par, and you don t need any conversion. You can find a full list of VB .NET data types and what they can hold in Appendix IV.


Talking about the API, if you re a heavy user , you ll be used to passing about fixed-length strings. Well, you don t have those in VB .NET. Previously, you would have declared a string like so:

 Dim MyString As String * 100 

But, in the VB .NET world, you ll need to add a special attribute to accommodate these, like so:

 <VBFixedString(100)> Dim MyString As String 

The information in <angle brackets> is an attribute. We ll discuss these more later.

Variant and Currency

Variant and Currency? You are the weakest link, goodbye ! Both of these aging data types have been thrown out the window. In VB .NET, the Object type can hold absolutely anything, so it s a good replacement for Variant. And the Decimal data type is a decent replacement for Currency, too.

Again, a full list of VB .NET data types and what they hold can be found in Appendix IV.

Array Alterations

Think you know arrays? Think again. In VB .NET, they ve changed.

The big alteration is that arrays are now always zero based. That means you can t define the lower boundary yourself: it s always zero. A typical declaration will now look something like

 Dim MyArray(9) As String 

Here, we have a string array containing ten elements ”from zero to nine. This also means that the VB6 LBound function is now pretty useless as it always returns 0.

The .NET Framework also contains new classes to help you handle "collections", such as the popular ArrayList . Some of these work like the VB6 Collection , while others work like simple lists or "queues". You can find out more about these by looking up "collections, .NET Framework" in the help index.

New Operators

Here s one cool change you ll soon get used to. Bringing VB .NET in line with other languages such as C++, you have new operators to shorten the way you write code. The most useful is +=, which can be used as so:

 intNum += 1 

This takes the value of intNum and adds 1 to it. It s the equivalent of the old

 intNum = intNum + 1 

You can also use other operators, as follows :

 intNum -= 4  ' intNum = intNum - 4  intNum *= 2  ' intNum = intNum * 2  intNum /= 6  ' intNum = intNum / 6 

The add operator works with strings also (and can be used interchangeably with the ampersand):

 strText += " - appended text" 

Declaring Properties

In the golden olden days, we d create properties as separate Get , Set , and Let blocks. In VB .NET, they ve been combined into one chunk , with a Get block to retrieve the property and a Set block to store it. Here s an example property in VB .NET:

 Dim mstrUsername As String    Public Property Username() As String        Get           Return mstrUsername        End Get        Set(ByVal Value As String)           mstrUsername = Value        End Set    End Property 

This property also demonstrates the Return keyword, which passes back a value in your property or function, then immediately exits.

User-Defined Types

If you ve worked with the API, you ll know it tends to call our user-defined types structures. Well, get used to it: the Type keyword has been replaced with Structure . Here s an example user-defined type in VB .NET:

 Public Structure MyStructure      Dim Username As String      Dim LogonCount As Integer  End Structure 

The more complex structures can also include their own creation parameters ( constructors ) and include methods , practically turning them into mini-classes.

Change the Scope

VB .NET now supports block-level scoping. If you typically declare all your variables at the top of your routine, you shouldn t run into any problems with this new feature.

But imagine this scenario: you declare a variable inside a loop and refer to it later outside the loop. In VB6, it s no problem. In VB .NET, however, it s outside the scope of the loop and therefore isn t available. It s something that s worth keeping in mind.

ByVal Is Default

By default, all parameters in VB .NET are passed ByVal (by value). In VB6, the default was ByRef (by reference). If you know what these keywords mean, you ll know how it might affect you, so make sure you change the ByVal keyword where required.

Set Has Disappeared

Virtually everything you use in VB .NET is an object. And, if you continued using VB6 syntax in VB .NET, that would mean having to use the Set statement to do something as simple as changing the value of a String variable. Not very sensible , I m sure you ll agree.

Therefore, they decided to scrap the Set statement altogether. After all, if everything is an object, the Set keyword serves only to annoy. You can still try using it, but VB .NET will automatically remove and attempt to spank you silly.

Also, most default properties have disappeared in VB .NET. You can no longer do something like:

 Dim MyString As String  MyString = TextBox1 

Why not? Have a think about it. If you don t have the Set statement, you can t have defaults. It s not an obvious connection, but imagine changing String here to Object . Now, exactly what are you trying to retrieve, a reference to the TextBox or the Text property? Have a ponder.

It s worth noting, however, that there is a way to create your own special default properties, such as an Item-style collection; however, as usual, certain rules apply. For more info , look up default properties for class in the help index.

Error Handling Changes

You can still use those favorite On Error Goto statements in VB .NET. However, there s also a new, more advanced way of dealing with errors, and it should be familiar to C++ and Java programmers. It s called exception handling , and it works by implementing a Try-Catch-Finally block. Here s a skeletal example.

 Try      ' potentially problematic code goes here  Catch      ' errors are caught here  Finally      ' optional, runs at the end,      ' whether an error occurred or not  End Try 

Here, we add our code under the Try block. Should any errors occur while running it, the code inside the Catch block kicks in. At the end, whether an error occurred or not, the code within the Finally block executes.

You can also add a little code to analyze the "caught" error, include multiple Catch blocks to handle specific error types, or ”by using the When keyword ”run error code only when certain conditions apply. We ll demonstrate some of these techniques in the Essentials section of Chapters 2 and elsewhere within this book. Look up catching exceptions in the help index for more information.

You can also raise and pass back an error in your code, by throwing an exception, like so:

 Throw New System.Exception("An error has occured!") 

The Class Keyword

Classes and their names are now defined according to the Class keyword. For example, here we re defining a class called Class1. We d put all our code somewhere in the middle:

 Public Class Class1  End Class 

This format also allows you to put multiple classes inside one file, which is useful if you re dealing with particularly small, data-holding or related classes. This format also applies for modules, which use the Module keyword as opposed to Class .

One more point: classes now have constructors and finalizers, as opposed to Class_Initialize and Class_Terminate . Also, as VB .NET is now a fully object-oriented language, you will find many enhanced object features, such as inheritance. We will be covering some of these techniques in this book. Alternatively, check out the MSDN help, or read the object discussions in my book, Karl Moore s Visual Basic .NET: The Tutorials (ISBN 1-59059-021-X) for more information.

The Ultimate VB .NET and ASP.NET Code Book
The Ultimate VB .NET and ASP.NET Code Book
ISBN: 1590591062
EAN: 2147483647
Year: 2003
Pages: 76
Authors: Karl Moore © 2008-2017.
If you may any questions please contact us: