Home > Microsoft .Net Development Tips > Visual Basic and Visual Basic .NET > VB.NET and 6.0 compared
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

VISUAL BASIC AND VISUAL BASIC .NET

VB.NET and 6.0 compared


Ed Tittel
01.21.2003
Rating: -4.43- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.


Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Visual Basic and Visual Basic .NET
Compose XML more quickly using Visual Basic 9
Virtualization keeps Legacy Apps alive
VB 9 Anonymous Types help create flexible objects
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Book Excerpt: Sams Teach Yourself Visual Basic 2008 in 24 Hours -- Complete Starter Kit
Check out CodePlex for a ton of interesting .NET projects
Book excerpt: Murach's VB 2008
Book excerpt: Printing in Visual Basic 2005
Visual Basic 2008 and closures
WinForms development using SQL Server 2005 and Visual Basic 2005

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts