Declaring and Using Variables
Click here to load reader
-
Upload
lani-norton -
Category
Documents
-
view
25 -
download
0
description
Transcript of Declaring and Using Variables
![Page 1: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/1.jpg)
Declaring and Using Variables
Visual Basic Data Types
![Page 2: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/2.jpg)
Naming Conventions
• Variables need to be assigned a data type and a name.
• The name should help you remember both the data type and purpose of the variable.
• Variable names can consist only of letters, digits, and underscores
intPopulation strCity
![Page 3: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/3.jpg)
Declaring Variables• To declare a variable you use the Dim
statement.• Syntax:
Dim variablename [As datatype]
• Examples: Dim strFname As String Dim strLname As String Dim strFname, strLname As String
![Page 4: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/4.jpg)
Type Stores Memory RangeByte (byt) Binary
numbers1 byte 0 to 255
Boolean (bln) Logical values 2 bytes true or false
Integer (int) Integers 2 bytes +/-32,768
Long (lng) Integers 4 bytes +/- 2 billion
Single (sng) Floating-pointnumbers
4 bytes -1E-45 to3E38
Double (dbl) Floating-pointnumbers
8 bytes -5E-324 to1.8E308
Currency(cur)
Numbers 8 bytes +/- 9E14
Date (dtm) Date and timeinformation
8 bytes Jan. 1, 100 -Dec. 31, 9999
Object (obj) Objectreference
4 bytes
String (str) Text 1 byte percharacter
Variant (vnt) Any data type Minimum16 bytes
![Page 5: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/5.jpg)
Option Explicit
• To require variable declaration, include the Option Explicit statement in the General Declarations section of a form or module.
• If you try to use a variable that you have not declared when Option Explicit is set, you will receive a run-time error.
![Page 6: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/6.jpg)
Conversion Functions
• Val() function converts a string to a number Dim intDegrees As Integer intDegrees = Val(txtTemp.Text)
• Str() functions converts a number to a string lblTemp.Caption = Str(intDegrees)
![Page 7: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/7.jpg)
Calculations ^ exponentiation - negation *, / multiplication and division \ integer division Mod modulus arithmetic +, - addition and subtraction Use parentheses to override the order of
precedence.
![Page 8: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/8.jpg)
Assigning Values to Variables
• If var is a variable, then the statement var = expression
first evaluates the expression on the right and then assigns it’s value to the variable.
![Page 9: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/9.jpg)
Example
Private Sub cmdCaluate_Click() Dim intNum1 As Integer Dim intNum2 As Integer Dim intAnswer As Integer intNum1 = Val(txtFirstInput.Text) intNum2 = Val(txtSecondInput.Text) intAnswer = intNum1 ^ intNum2 picAnswer.Print intAnswer End Sub
![Page 10: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/10.jpg)
PictureBox Print Method
• PictureBoxes can be used for output with the Print method:picAnswer.Print intNum1
picAnswer.Print intNum2
• Use a semicolon to print on the same line:picAnswer.Print intNum1;
picAnswer.Print intNum2
orpicOutput.Print intNum1; "+"; intNum2; "="; intAnswer
![Page 11: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/11.jpg)
PictureBox Cls Method
• PictureBoxes can have their contents removed with the Cls method:picOutput.Cls
![Page 12: Declaring and Using Variables](https://reader038.fdocuments.in/reader038/viewer/2022100501/56812da5550346895d92c8cd/html5/thumbnails/12.jpg)
Errors
• Syntax errors– usually grammatical errors or misspellingpicAnswer.Primt intAnswertxtAnswer.txt = intFirstInput + intSecondInput
• Runtime errors– errors detected while the program is runningdblAnwer = 1/intFirstInput
• Logical errors– errors which occurs when the program does not run as intendeddblAverage = intFirstInput + intSecondInput/2