ARTICLE

How to declare a variable in Block Scope

Posted by Manish Tewatia Articles | Visual Basic Language July 26, 2010
This article helps you understanding the declaration of variable in block scope
 
Reader Level:

Variable declaration is a great new way to make your application performance better like a new feature Block level scope declaration in this you can declare the variable for entire procedures when all you need is a counter in a block. The advantage of using block scoped variables is that they go out of scope once your code exists the block of code in application .

Example Code

    Module Module1
        Sub Main()
            Dim A As Integer
            For
A = 1 To 5
                Dim N As Long
                N = N + A
            Next
            Dim
J As Integer
                For
J = 1 To 4
                    For A = 1 To 5
                        Dim b As Long
                        Console.WriteLine("Value: {0}", b)
                        b = b + A
                    Next
                Next
            Console.ReadLine()
        End Sub
    End
Module

OUTPUT

1.gif

CONCLUSION

In this article you can see how to declare block scope variable and like the output you saw b retains its value between executions of the inner loop.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor