Você está na página 1de 13

Data Types, Variables

and Constants
FOR LE1, LE2 AND LE3
Numeric Data types
Non Numeric Data types
Suffixes for Literals
• Literals are values that you assign to a data.
• In some cases, we need to add a suffix behind a literal so that Visual Basic
2012 can handle the calculation more accurately.
• For example, we can use num=1.3089# for a Double type data.
Rules in Naming Variables
• It must be less than 255 characters
• No spacing is allowed
• It must not begin with a number
• Period is not permitted
Declaring Variables
Dim Variable Name As Data Type

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
Dim password As String
Dim yourName As String
Dim firstnum As Integer Dim VariableName as String * n,
Dim secondnum As Integer where n defines the number of
Dim total As Integer characters the string can hold.
Dim doDate As Date
End Sub
Declaring a Variables
Dim
Dim number1 As Integer
Dim number2 As Integer • Short for Dimension.
number1
number1 = 3 • This is a variable
number2 = 5
As Integer
OR • We're telling Visual Basic
that the variable is going to
Dim num1, num2 As Integer be a number (integer)
num1 = 3 number1 = 3
num2 = 5 The = sign means assign a
value of…
Assigning Values to Variables
Variable=Expression
• The variable can be a firstNumber=100
declared variable or a secondNumber=firstNumber-99
control property value. userName=”John Lyan”
• The expression could be userpass.Text = password
a mathematical Label1.Visible = True
expression, a number, a Command1.Visible = false
string, a Boolean value Label4.Caption = textbox1.Text
(true or false) and etc. ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber+ThirdNumber
Constants
• Constants are different from variables in the sense that their values
do not change during the running of the program.
• The syntax to declare a constant is
• Const Constant Name As Data Type = Value
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const Pi As Single=3.142
Const Temp As Single=37
Const Score As Single=100
End Sub
Mathematical Operations
Textbox Properties
txtFName.text = variable
- Store the content of the variable
txtFName.Clear()
- Clear the content of a textbox
txtFName.Visible = False
- Textbox is not visible unless it is True
Example
Design a program that will use two textboxes and some labels that will compute
the sum, difference, product and quotient of two numbers.
Be able to display the answer in four labels after a button is click.
Example
Design a program that will compute the Area of a circle.
Formula is A = 𝜋𝑟^2

Você também pode gostar