Constants in Visual Basic .Net

Last Updated: March 20, 2022

In programming constant is like a variable but a constant is not going to change once you define it

You can define the constant like this

Const PI As Double = 3.14

So the value of PI is not going to change so it is a constant

Create a function to calculate the area of a circle and pass the radius as a parameter to the function

    Const PI As Double = 22 / 7
    Function GetArea(ByVal radius As Double) As Double
        Return PI * radius * radius
    End Function

Here I have defined the PI outside the function but I am using it inside the function.

If you try to change the value of PI inside the function, you will get compile time message
“Constant can not be the target of an assignment”