VB.NET Uncovered: Big Changes, Page 6
Coding whatnots. What has really changed that you need to know about?
Well, apart from the namespaces thang, let's review the stuff that will affect you:
Longs, Integers - Our old Long has turned into anInteger and the Integer has turned into something called Short. The new Long inVB.NET holds a 64-bit number
Byte, Char - The new Byte data type can hold a numberbetween 0 and 255. The new Char data type consumes 2 bytes of space and canhold one Unicode character.
Goodbye Variants - Variants have disappeared. In itsplace is the generic Object data type, which can now hold virtually anything(it ain't as memory-intensive as VB6 neither)
Currency Replacement - The Currency data type no longer exists in VB.NET and is replaced by the more powerful 128-bit Decimal data type
Variant Declaration - In VB6, "Dim X, Y, Z As Integer" would result in two Variants and anInteger. Yet in VB.NET, this gives us three Integers (which, remember are our old Longs!)
Zero-Based Arrays - This is something a lot of people have complained about and is subject to change. Arrays in VB.NET are zero-based - meaning they always start at 0. So code such as "Dim MyArray(10) As Integer" would result in an Integer array of eleven elements - zero through to ten
Top Tip: To declare variables (+ arrays!) that have a "form-level" scope, place them before the 'New' method automatically created for you in the code window. This is the old Declarationsarea.
UDT Changes - User-defined types are awfully useful. But when declaring in future, you need to use the API-like keyword Structure instead of Type. So here's how you would declare a sample UDT:
Structure MyStructurePublic Name As StringPublic Age As ByteEnd Structure
Collections Gone - VB.NET doesn't support the Collection object as we know it. Instead, it provides you with a bunch of new collection types in the System.Collections namespace - the most similar are HashTable and ObjectList. However you can use the old Collection object using the Compatibility namespace, like this:
Dim MyCol As Microsoft.VisualBasic.Compatibility.VB6.Collection
New Operators - VB.NET brings with it a few new arithmetic operators to help cut down your code. For example, "X += 4" in VB.NET will do what "X = X + 4" did in VB6. Try playing with these too: "X -= 10, X *= 2", "X /= 13", "X \=13", "X ^= 3", "X &= " OK"
Short Circuiting - VB.NET short circuits If...Then statements. So, if you have two parts of an If...Then statement and the first returns False, VB.NET doesn't bother to look at the second part
No Set - In VB6, we used the Set statement quite often - it was the one thing that set objects apart from any other regular data type. In VB.NET, everything is an object - so there's no need for it. If you do type it in, VB.NET currently removes it for you. How nice
Property Declarations - Properties are now declared differently - no more separate Lets and Gets. Here's an example of a new property procedure... note that Value is now a keyword that always contains the value passed to this property.
Public Property MachinePart() As StringSetmstrName = ValueEnd SetGetMachinePart = mstrNameEnd GetEnd Property
Error Handling - Even error handling has changed in VB.NET. Here, you use a Try, Catch and Finally structure. The code within the Try block is run - if an error occurs, code in the Catch block is run. Whatever happens, the Finally block is always run. This is a strange concept to VB programmers yet is common practice to anyone familiar with C or Java. I recommend you use the VB.NET help index to find Error Handling, Overview - "Introduction to Exception Handling". Be sure to check out the 'Try...Catch...Finallystatement' section and test the example provided.
Default ByVal - By default, all parameters are now passed 'by value' as opposed to 'by reference'. To be safe, make all declarations explicit
It's No .Show - There's no longer a simple Form.Show method. Everything in VB.NET is an object - so you need to actually 'Dim FormName As New Form1', then do a 'FormName.Show'
Control Arrays - Hasta la Vista, Guten Tag and all that, baby
Garbage Collection - Although not a coding change, Garbage Collection is a process that runs when the operating system (slash .NET Framework) thinks it's time to clear up object references and such. So in VB6, when you set an object to Nothing, it immediately disappears from memory. However in VB.NET, this doesn't happen straight away - and your object may stay 'live' for a few minutes before being terminated by the Garbage Collector - so you can never be too sure when class termination code will run! Strange though it may seem, this 'nondeterministic finalization' does have its benefits - such as automatically correcting circular references. Check out the help for more (if you're really that interested <snore>)
Return Keyword - Inside a function, instead of setting the function name to your return value or object, you can simply state "Return MyData" allowing you to perhaps change the function name without altering the actual code
Top Tip: I can't hope to cover all of the syntax changes here, though hope to have presented what I deem to be the most important. To find out more on any particular subject, check the help. And of course, if you have any important additions - post a message at the feedback forum.
Another Top Tip: - Don't forget, much of the old VB6 functionality is still available to you in VB.NET - simply refer to them via the Microsoft.VisualBasic.Compatibility.VB6 namespace. However, asever, it's better if you can move along to the newer, more generic functions.
Page 6 of 7