Constants in Visual Basic .Net

August 23, 2017

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

You can define the constant like this

Const PI As Double = 3.14

 

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

I am going to create function to calculate the area of a circle. I will 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 target of an assignment”