VB.NET and 6.0 compared

A look at terminology changes between the two languages.

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!


Recently, I've fielded several reader questions about specific differences between Visual Studio .NET and earlier implementations, as well as language differences between earlier and .NET versions. In researching the answers, I found a terrific resource in Microsoft TechNet -- namely KB article 311327 entitled "New Data Types in Visual Studio .NET." This handy resource not only explains key differences in the way variables are declared and instantiated across the different implementations, but also provides lots of detailed examples about how things change from prior to .NET versions.

A brief discussion of string data types provides a good example of such changes, but the KB article provides detailed, exhaustive coverage of the whole panoply of differences. Outwardly, String data types look the same in Visual Basic .NET as they do in Visual Basic 6.0. But whereas String data types are variable in VB 6.0, they are invariant in VB.NET.

In fact, String data types are read-only after they've been initialized. It's a subtle but important point to recognize that this means it's impossible to directly modify a String typed value's contents. A String variable contains a pointer to memory where the actual associated string resides. If that value is modified, the underlying runtime systems deallocates the memory block that contains the original value, and allocates a new memory block where the changed (new) value resides. Consider the following code block:

Dim Sstrng as String
Sstrng="Hello"            'Allocates memory for "Hello".
Sstrng=Sstrng & " World"   'Deallocates memory for "Hello" and
                            reallocates memory for "Hello World".

Here's how the two environments differ, even though VB handles string memory the same for both VB 6.0 and VB.NET. In VB, before .NET came along, developers could user the Mid statement to directly modify contents of string memory like this:

Dim Sstrng as String
Sstrng="Hello World"       'Allocates memory for "Hello World".
Mid(Sstrng,7,5) ="There"   'Modifies memory to insert There 
                            between Hello and World

But the Mid statement in VB.NET works by concatenation, so that it actually deallocates the previous value, performs the requested operation, and then reallocates the updated string value just like all other VB string operations. This differs from the "change in place" approach that VB 6.0 used.

Microsoft goes on to explain the advantages and disadvantages of the String data type's invariance as follows:

  • Pros
    1. Copying the string means copying the pointer to the string. Modifications create a new instance and leave the old one alone. If no variable points to the old memory block, garbage collection cleans it up automatically.
    2. Because String variables are invariant in VS.NET, memory buffers are read-only and need not be locked in multi-threaded environments to avoid contention. Multiple threads can share such values with no performance impact.
  • Cons
    1. Constant or frequent deallocation and reallocation of string memory can be costly, and will incur performance issues.

Microsoft goes on to recommend using the StringBuilder class in VS.NET in cases where string values change frequently at runtime. This approach offers the same benefits to VS.NET as the original implementation of the Mid statement in VB 6.0. The observed threshold for taking this approach is for values that will be changed or concatenated 300 times or more while an application is active. For those in doubt, a simple counter should be incremented at each update, and can help you determine if you should take the alternate approach.


Ed Tittel is a principal at LANWrights, Inc. a wholly-owned subsidiary of iLearning.com, where he writes and teaches on a variety of subjects, including markup languages, development tools, and IT certifications. Contact Ed via e-mail at etittel@lanw.com.


This was first published in January 2003

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close