Constants in VB.NET

How to use Constants in VB.NET?


Constants in VB.NET

Constants in VB.NET 2008 are declared using the keyword Const. Once declared, the value of these constants cannot be altered at run time.

[Private | Public | Friend | Protected Friend ]
Const constName As datatype = value

In the above syntax, the Public or Private can be used according to the scope of usage. The Value specifies the unchangable value for the constant specifed using the name constName.

Public Class Form1
Public Const PI As Double = 3.14159
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim r, a As Single
r = Val(TextBox1.Text)
a = PI * r * r
TextBox2.Text = a
End Sub
End Class

In the above example, a constant PI is declared as Public so that it can be used anywhere in the class.

Visual Basic Tutorial

Ask Questions

Ask Question