Variables and Constants
VB --- Variables and Constants
A variable is a named memory allocation that stores a value. Before using a variable it should be declared with a Dim statement that includes the identifier and data type. The identifier or name of the variable is how the variable will be referred to throughout the program; the data type of the variable indicates what kind of value it will store.
Some common data types include:
|Type |Used to represent |Memory Requirements |
|Single |Numbers possibly containing decimals |4 bytes |
|Double |Numbers possibly containing decimals |8 bytes |
|Integer |Integers (no decimals) |2 bytes |
|Long |Integers (no decimals) |4 bytes |
|Currency |Numbers representing dollar amounts |8 bytes |
|Char |One character – letter, symbol, number |1 byte |
|String |A set of characters |1 byte/character |
|Boolean |True or False |2 bytes |
The Dim statement is used to define variables
Example: Dim dblResult As Double ‘would reserve room in the computer’s memory for a number.
Multiple statements can be put on the same line
Example: Dim dblResult As Double , strName As String, intAge As Integer
A constant is a named memory location which stores a value that cannot be changed at run time from its initial assignment. The use of named constants is good programming style because they give unchanging values a meaningful name and make the program code easier to read.
Example: Const Pi As Double = 3.14 ‘ give Pi a value of 3.14
Here is an example using Dim and Const
Private Sub cmdCalculate_Click()
'when this button is clicked it will calculate the area of a circle with radius = 10
Const Pi As Double = 3.14 'define Pi as a constant
Dim dblRadius, dblCircleArea As Double 'define variables - as doubles
dblRadius = 10 'assign value of radius
dblCircleArea = Pi * dblRadius ^ 2 'note the math symbols used
lblAnswer.Text = dblCircleArea 'display the area
End Sub
** Note: you can not change constant assignments in the program, for example:
Pi = 22/7 ‘ this would result in an error
Choosing Identifiers
Legal VB identifiers are not case sensitive but:
• Must begin with a letter
• Must contain only letters, digits, and the underscore(_) character. (periods, spaces and other special characters are not allowed)
• Cannot exceed 255 characters
As a matter of good programming style, variables and constants should be named so that they are quickly and clearly understandable to the reader. This includes using a prefix that describes the type of data the variable or constant will store. Ex. DblRadius tells us that radius is a variable of type double
The Text Box
An application can be more useful when the user is able to input values at run time for use in the program code. For example, the Circle Area application would be more useful if the user could enter the radius value for calculating the area of the circle. A text box allows this. A label is often placed beside the text box to describe its contents or purpose.
A change event procedure is usually coded for each text box. The change event procedure is executed when the user begins to type in a text box
Private Sub txtRadius_TextChanged()
‘ clears current answer when the user begins to type in a new radius
lblAnswer.Text = ""
End Sub
Data typed in a text box is a String data type. Because VB automatically converts between data types, the string entered by the user can be assigned a numeric data type for use in a calculation. This will work as long as the value entered can be transformed into a numeric value, if the user had entered “abc”, an error would have occurred because “abc” is not a numeric value.
Check for Knowledge
1. List three data types.
2. You want a variable to store a person’s age – What is the best type to use? Why?
3. Write the declaration statement necessary to define a variable called blnplayagain.
4. Identify two names that cannot be used as a variable name. Explain why for each.
5. What are the values of the visible property? Write a vb statement to change the value.
6. Identify two properties of the textbox control. Write a piece of code to change the values of those properties.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- constants vhdl
- variables and expressions worksheets
- constants in excel formulas
- polynomial constants fit model
- algebra variables and expressions worksheet
- types of variables and examples
- constants and control
- what are independent variables and dependent
- algebra variables and expressions
- algebra variables and expressions answers
- algebraic variables and expressions
- python print variables and text