Home > Microsoft .Net Development Tips > Visual Basic and Visual Basic .NET > The block-scoping scoop
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

VISUAL BASIC AND VISUAL BASIC .NET

The block-scoping scoop


Ed Tittel
02.04.2003
Rating: -3.50- (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!


If you're trying to use your VB 6.0 based programs in a Visual Basic .NET environment, you could wind up with some unexpected error messages. One reason is that there are some differences between how variables are scoped between Visual Basic 6.0 and Visual Basic .NET, and the difference may affect how code written to VB 6.0 works in a VS.NET/VB.NET environment.

The basic principles are pretty simple, but the implications can be far-reaching. Fortunately, there is an informative error message that occurs when the new block scoping rules are violated, and fixing such problems is also relatively straightforward, through various means.

To begin with, scoping refers to the portions of code or procedures in which variables are defined (and thus, where they will normally have associated values). In VB 6.0, a variable can be declared anywhere inside a procedure and has full scope inside the entire procedure no matter where it may be declared (Microsoft calls this "full procedure scope"). In VB.NET variables declared within code blocks may be referenced only inside that code block, where a code block is defined as any set of statements that end with an End, Loop, or Next statement (Microsoft calls this "block scope").

It's not that the variables do not exist or contain no values outside the code blocks within which they have scope; it's only that for VB.NET such variables may be referenced only inside the block in which they're defined.

The following code example is legal in VB 6.0, but generates an error in VB.NET:

Dim k As Long
For k = 0 to 5
 Dim counter As Long
 counter = counter + 1
Next k
MsgBox(counter)

In fact VB.NET returns an error message that states "variable 'counter' has not been declared" and occurs because counter is declared in the For statement block (from the For through the Next statement) but is also referenced outside that block in the last line of code in the example.

There are an infinite number of workarounds possible, but two will work quite nicely:

  • If a variable needs true procedure scope, it should be declared at the beginning of the procedure before any code blocks are defined.
  • If a block scope variable must be referenced outside the block, its value can be assigned to a procedure scope variable inside the block.

Either way, this provides a way to access that value outside the scope of a block variable if that should be necessary. The error message makes it easy to tell what's gone wrong, and it should be easy to restructure code in one of the two ways recommended to get things working in VB.NET.


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