The Essential .NET Data Types

  • 8/15/2011

Constants and Read-Only Fields (Read-Only Members)

Apart from the variable types discussed in this chapter, there are two value storage types in Visual Basic, which at first glance seem rather similar, constants and read-only fields. These two value storage types are defined only once during your program’s lifetime, but they behave very differently under certain circumstances—and their contents are saved in completely different manners.

Before discussing the differences, let’s examine what they have in common. Read-only fields and constants are used in similar contexts: namely, when a value must be used at different places within a program. For example, you would probably use a constant to return the name of your program or the expiration date of a demo version. When queried at different places within your program the constant must always have the same value. To avoid having to change the value in the source code in many different places (should you need to make any changes later on) you define this value as a constant or read-only field centrally, and then substitute the field or constant name for the value where you would use the value in the code.

Of course, you need to ensure that this value cannot be overwritten. Therefore, neither constants nor read-only fields can be changed.


You define constants with the Const keyword at the module level. Module level means that you can define a constant in a class, in a module, or in a structure. The syntax for defining a constant is just like a variable, but you need to add the keyword Const, as shown in the following example:

Public Const APPLICATIONNAME As String = "Type demo"


Private Const EXPIRATIONDATE as Date=#12/31/2010#

You can also define other constants this way—you just need to specify the appropriate type with the As clause and a value.

But watch out!

You can only define actual constants as constants. Even if, for example, the return value of a method, such as Date.MaxValue actually has the characteristics of a constant, you can’t assign it to a constant. For example, the following statement will cause an error:

Private Const EXPIRATIONDATE as Date=Date.MaxValue

In this case, you should use a read-only field, as explained in the following section.

There’s another important issue—the use of public constants across several assemblies.

Read-Only Fields

Read-only fields can also define constant values. These are also defined exclusively at module level (class, structure, module), and differ from a typical variable declaration by the keyword ReadOnly:

Private ReadOnly THEDATE As Date=#07/24/1969#
Friend ReadOnly MAXDATE As Date=Date.MaxValue


Public ReadOnly PERMITTED_CITIES As New List(Of String) From {"Lippstadt", "Las Vegas",
                                                             "Kempen", "Los Angeles"}

These examples show that unlike regular constants, read-only fields are executable statements.

Therefore, the following construction is permitted:

Public Class Test
    Friend ReadOnly MAXDATE As Date
    Public ReadOnly PERMITTED_CITIES As List(Of String)

    Sub New()
        'Permitted only once!
        MAXDATE = Date.MaxValue
        PERMITTED_CITIES = New List(Of String) From {"Lippstadt", "Las Vegas",
httpatomoreillycomsourcemspimages882161.jpg"Kempen", "Los Angeles"}
    End Sub

If you attempt to assign or re-assign a value to a read-only field from within a method, as in the following code segment, Visual Basic generates a design-time warning. The following code will cause the error “A read-only variable cannot be the target of an assignment.”

Sub NewMethod()
    MAXDATE = Date.MaxValue
    PERMITTED_CITIES = New List(Of String) From {"Munich", "Paris", "London", "Seattle"}