![Page 1: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/1.jpg)
Data Types and Variables
![Page 2: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/2.jpg)
Data Type!
Computers are all about Data!
Data can be in the form of Text Dates Sounds Pictures
![Page 3: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/3.jpg)
Visual Basic supports the following data types:Data Type RangeWhole Number:Integer -32,768 to 32,767Long -2,147,483,648 to
2,147,483,647Decimal Number:Single -3,402823E+38 to
3,402823E+38Double -1,79769313486232E+308
to 1,79769313486232E+308
![Page 4: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/4.jpg)
Data Type Range
Other Types:
String 1 to about 65,000
characters
Date January 1, 100 to
December 31, 9999
Boolean True or False
![Page 5: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/5.jpg)
Variable!
Definition:
Memory locations where temporary data is stored.
![Page 6: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/6.jpg)
Facts about Variables.
Variables are a common feature of programming languages.
Variables can be used to store and manipulate all kinds of data.
Variables get their name from the fact that the value can vary as the program runs.
You can assign values to variables using the assignment operator. (=)
You should declare variables before you use them.
![Page 7: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/7.jpg)
Declaring Variables
The first step to using a variable in your program is to:
1. Let the compiler know that you want to set up a memory location as a variable.
2. What you want to call the variable.
3. What data type you want the variable to have. The above process is called declaring a
variable!
![Page 8: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/8.jpg)
Example:
To declare a variable, use the Dim statement as show below.
Dim VariableName As DataType
The following declare a variable named intAnswer with the Integer data type.
Dim intAnswer As Integer
![Page 9: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/9.jpg)
Rules for Naming Variables!
When naming variables, keep the following rules in mind.
1. Variable names must begin with an alphabetic character ( a letter).
2. Following the first character, letters, numbers, and the underscore (_) are allowed.
3. Variable names cannot include any spaces. 4. Variable names can be up to 255 character
long.
![Page 10: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/10.jpg)
Using the Prefix Rule!
Prefix Data TypeExample
int Integer intPeople
lng Long lngInches
sng Single sngWeight
dbl Double dblMass
str String strName
dte Date dteAnniversary
bln Boolean blnSold
![Page 11: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/11.jpg)
Example Program:
Open VB and create a new form the looks like the following:
![Page 12: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/12.jpg)
Add 5 labels. Set the name properties to the following:
Label1 should be lblAgeYears
Label2 should be lblMonths
Label3 should be lblYou
Label4 should be lblOutputMonths
Label5 should be lblAgeMonths
![Page 13: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/13.jpg)
Set the Name property of the form to frmMonths and the caption to Month Converter.
Set the captions of the five labels to:
Age in years:
Months since last birthday
You are
X
months old.
![Page 14: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/14.jpg)
Add two text boxes to your form. Set the name of the text boxes to
txtYears
txtMonths
Add one command button. Set the name of the command button to
cmdMonths Set the caption to Calculate age in months
![Page 15: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/15.jpg)
Change the properties of the following labels to false.
lblYou, lblOutputMonths, lblAgemonths
Double-click the Calculate age in months button.
Key Dim intMonths As Integer at the top of the event procedure and press the Enter key twice.
![Page 16: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/16.jpg)
Enter the following:
'Calculate Months
intMonths = (Val(txtYears.Text) * 12) + _
Val(txtMonths.Text)
'Display Number of Months Old
lblOutputMonths.Caption = intMonths
lblYou.Visible = True
lblOutputMonths.Visible = True
lblAgemonths.Visible = True
![Page 17: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/17.jpg)
Run and fix your errors. Enter 5 in the Age in years text box. Enter 4 in the Months since last birthday text
box. Click the Calculate age in months button.
What is your output?
![Page 18: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/18.jpg)
Summary
Data can be in the form of numbers, text, dates, pictures, and even sound.
Visual Basic supports a set of data types. There are data types for whole numbers,
floating point numbers (decimals), text, dates, and more.
You can choose to store data in memory locations called variables.
![Page 19: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/19.jpg)
Summary
The first step to using a variable is to declare it using the Dim statement.
When naming variables, keep the naming rules in mind. It is also a good idea to use naming prefixes to identify the data type of the variable.
You should declare variables before you use them.
You can assign values to variables using the assignment operator (=).
![Page 20: Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures](https://reader031.vdocument.in/reader031/viewer/2022013012/56649f275503460f94c3fe50/html5/thumbnails/20.jpg)
Homework:
Answer the following questions:
1. What is the DIM statement used for?
2. What are the two common integer data types?
3. What are the two common decimal data types?
4. Write Visual Basic code to declare a String variable called strFirstName.