Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Visual Basic Language » Constants in VB.NET

Constants in VB.NET

In this article I will explain about Constants in VB.NET.

Author Rank :
Page Views : 1269
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


HTML clipboard

A constant is a variable modifier that represents a constant value, a value that can be computed at compile-time. A constant declaration introduces one or more constants of a given type.

A constant declaration can declare multiple constants (as in Listing 5.18) in a process that is equivalent to multiple declarations of single constants with the same attributes, modifiers, and type.

Listing 5.18: Constants Example 1


    Class A
        Public Const X As Integer = 1, Y As Integer = 2, Z As Integer = 3
    End Class

    Class B
        Public Const X As Integer = 1
        Public Const Y As Integer = 2
        Public Const Z As Integer = 3
    End Class

Evaluating constant values in complex class hierarchies can be a bit confusing and warrants closer examination.

The declaration of constants can depend on other constants within the same program (see Listing 5.19) as long as the dependencies are not of a circular nature. The .NET framework automatically arranges constant declarations to evaluate the declarations in the appropriate order.

Listing 5.19: Constants Example 2


    Class A
        Public Const X As Integer = B.Z + 1
        Public Const Y As Integer = 10
    End Class

    Class B
        Public Const Z As Integer = A.Y + 1
    End Class

In this example, the compiler first evaluates Y, then Z, and finally X, producing the values 10, 11, and 12. Constant declarations may depend on constants from other programs, but such dependencies are only possible in one direction. Referring to the example above, if A and B were declared in separate programs, it would be possible for A.X to depend on B.Z; however, B.Z could not then depend on A.Y.

A static read-only field is useful when a symbolic name for a constant value is desired—for example, when the type of the value is not permitted in a constant declaration or when the value cannot be computed at compile-time. Constants and read-only fields have different binary versioning semantics. When an expression references a constant, the value of the constant is obtained at compile-time. When an expression references a read-only field, the value of the field is not obtained until runtime.

Listings 5.20, 5.21, and 5.22 present a solid example of the difference between const and read-only in real-world applications.

Listing 5.20: ConstOut.VB , Constant Usage Example


    Imports System
    Public Class A
        Public Const X As Integer = 123
    End Class

Listing 5.2: ReadOnlyOut.cs, ReadOnly Usage Example

    Imports
System
    Public Class B
        Public Shared ReadOnly X As Integer = 123
    End Class

Listing 5.22: ReadOnlyConst.cs, ReadOnly Const User Class Example

    Imports System
    Public Class MyTest
        Public Shared Sub Main()
            Console.WriteLine("A.X value = {0}", A.X)
            Console.WriteLine("B.X value = {0}", B.X)
        End
Sub
    End Class

The code in these three examples produces the display shown in Figure 5.7.

Figure 5.7: Screen Output Generated from Listings 5.20, 5.21, and 5.22

fig_5.7.gif

If you modify X in ConstOut.cs, then you have to compile both ConstOut.cs and ReadOnlyConst.cs consecutively. But if you modify X in ReadOnlyOut.cs, then you only need to compile ReadOnlyOut.cs; there is no need to recompile MyTest.cs. However, the execution speed of const is better than that of the static read-only.

Conclusion

Hope this article would have helped you in understanding constants. See other articles on the website on .NET and C#.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dinesh Beniwal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.