Constants

Submitted by: 

In this tutorial, I’m going to show you how to use constants. In the real world, there lots of value that never changes like a Day it is always a 24 hour, and a sqaure has always four equal sides. This value remains constant. It refers to a fixed value that the program may not alter during its execution. The constants are just like regular variables except that their values cannot bit modified after their definition.

Constants Declaration

To declare constant using visual basic, use “Const” statement. This statement is used at module, class, structure, procedure, or block level for use in place of literals.

Defining one or more constants

[ ] [ accessmodifier ] [ Shadows ]
Const constantlist

attributelist – the specified list of attributes applied to the constants. And we can provide multiple attributes separated with a comma.
accessmodifier – it specifies the code to access these constants, it could be public, protected, Friend or Protected Friend or Private.
Shadows – it is use to hide and redeclare a programming element in a base class.
constantlist – it provides the list of constant names declared in the statement.

Here are the following Syntax and Parts of each constant.

  1. Constantname [ As datatype ] = initializer

Constantname – a Specified Constant name
Datatype – the data type of a constant
initializer – the assigned value of a specific constant

Example:
This example is to compute the area of a circumference.

  1.  
  2. Const PI = 3.14149
  3. Dim radius As Single
  4. Dim area As Single
  5.  
  6. radius = 5
  7. area = (radius * radius) * PI
  8. MsgBox("The area of circumference = " & Str(area))

Output:


Comments

very nice, but pi value = 13.14159 :D just noticed

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.