4.01 Understand Variables Final
4.01 Understand Variables Final
Naming Conventions
COMPUTER PROGRAMMING I
Objective/Essential Standard
COMPUTER PROGRAMMING 1
Variable
What is a variable?
Variables
What is a variable?
Simply put- a variable is a named place in computer memory
that holds a value. The user (who runs the program) or the
programmer (coder) can supply the value of a variable.
Example
Dim intNumber as Integer
Variable Basics
Do not capitalize the prefix but do capitalize the first letter of
each subsequent word.
strLastName
intStdNum
Example:
Dim intNumber as Integer
When a numeric variable is declared, its initial
value is 0 unless it is given a value at the same time.
When a string variables is declared without a value,
its initial value is null.
The compiler will not like this.
Initialize Variables
Variable Assignment
Name =
Keyword
before the DataType
Data Type
Assignment Statement
intSide
Creates the
place in 10
memory
For example:
strName=“Alex”
chrLetterGrade= “A”
strData = “This is some data “ & intNum & “and this is a number.”
Variable Scope
The time that the variable is available for use is called its lifetime.
1. Global
Can be called anywhere in the program
2. Local
Can only be called in the sub/function it is declared in
3. Procedural
Can only be used in the block of code it is declared it
Example: within an IF statement or loop
Variable Scope
Example
label.Text = variable.ToString(“format”)
For example:
lblTotal.Text = decTotal.ToString("$##.00")
ToString Formats
http://
msdn.microsoft.com/en-us/library/dwhawy9k.aspx
#Y2728
Getting Input from the User
TextBox properties:
The label placed near the text box to describe its contents
or purpose is called a prompt.
Using the TextBox Control
intNum = Convert.ToInt32(strNumInput)
intNum = Convert.ToInt32(txtNumInput.Text)
dblGpa = Convert.ToDouble(strGpaInput)
dblGpa = Convert.ToDouble(txtGpaInput.Text)
Input Does Not Match Variable DataType
32
Result: Error
Using a TextChanged Event
33
MESSAGEBOX
Display Output
MessageBox.Show("Your name is: " & strName & " ." &
“You are " & intAge & "years old.")
End Sub
Advanced Variables
Variable Global
Data Type Local
Integer Procedural
Double Output
Decimal ToString
Char TextBox
Boolean Prompt
String MessageBox
Literal Static Variable
Hungarian Notation Constant Variable
Concatenation Counter
Scope Assignment
Code
COMPUTER PROGRAMMING 1
Naming Objects
What is an object?
Objects represent “real” things
Dog, Chair…
Label, Button
Objects have properties and behaviors (methods)
Button
Properites: Name, Text …
Behaviors/Methods: Click . . .
Naming Objects
Ex: Me.Close
Preceeding a form name with “Me” refers to the
current form (The active one).
Using Me is optional in VB 2010 in most cases
when using only one form.
When using multiple forms, opening and closing
forms must use the Me.Hide or Me.Show.
Object
Naming Conventions for our objects