Variables Strings Review Data appearing as numbers are called Numeric literals Arithmetic Operators Addition Subtraction Multiplication Division Exponentiation Grouping Symbols ID: 428404
Download Presentation The PPT/PDF document "CS0004: Introduction to Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS0004: Introduction to Programming
Variables –
StringsSlide2
Review
Data appearing as numbers are
called…
Numeric
literals.
Arithmetic Operators
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation ^
Grouping Symbols
()
A method is…
A process that performs a task for a particular object
Variables are…
Named memory locations that can hold values
A method is
…
A
process that performs a task for a particular
object
A function is…
A method that returns a
valueSlide3
Review
A parameter is…
Information passed to a method or function to be
processed
Declaration Statement General Form:
Dim
variableName
As Type
An assignment statement …
Puts a value into a variable. Uses the assignment operator (=)
Some integer arithmetic operators
Integer Division (\)
Modulus (Mod)
More complex math operations
Math.Sqrt
()
Math.Round
()
Int
()Slide4
String
A
s
tring literal
is a sequence of characters that is treated as a single item
Examples:
“Banana”
“It can have spaces”
“It can have special characters!!^$&(”
“”
This is the
empty string
a string with no characters
String literals are surrounded in double quotes (
“ ”
)
A
string variable
is a variable that holds a string (is of the string data type)
To declare a string variable:
Dim
variableName
As String
You can also initialized a string variable
Dim
variableName
As
String = “banana”
And also declare more than one on a single line
Dim
variableName1, variableName
2
As StringSlide5
String Example
New Topics:
String
d
eclaration
String literal
String variableSlide6
Text Boxes
The content of a text box is always a string, so you can do things such as:
Dim answer As String
answer =
txtAnswer.Text
Or…
txtAnswer.Text
= answer
However by default you can do things like:
Dim number As Integer
number =
txtAnswer.Text
This is known as
implicit typecasting
: THIS IS BAD PROGRAMMING PRACTICE!
Typecasting
– converting from one type to another
To prevent the urge to do this you should put
Option Strict On
at the top of the code to your form
This will make the compiler not allow implicit typecasting
Also, you should put the line
Option
Explicit On
at the top
This will make the compiler not allow you to use a variable before you declare itSlide7
Explicit Typecasting
So what if we want to take user input data that is a number?
Answer: Explicit Typecasting
You can use a pre-defined function to explicitly typecast a string to a double or integer or vice versa:
CDbl
(parameter)
– typecasts to double
CInt
(parameter)
– typecasts to integer
CStr
(parameter)
– typecasts to string
If you do some calculation with a number, and you want to put the result in a text box, you should typecast the number into string first
txtBox.Text
=
CStr
(result)Slide8
Concatenation
You can combine multiple strings into one string using the
concatenation operator
(&)
Dim groceryList1 As String = “bread, milk, ”
Dim groceryList2 As String = “eggs, apples”
Dim
fullGroceryList
As String
fullGroceryList
= groceryList1 & groceryList2
You can also concatenate strings with number data types
Dim
winnerString
As String =
“And the winner is contestant #”
Dim
winnerNumber
As
Integer
= 2
Dim
winner
As String
winner
=
winnerString
&
winnerNumber
There is a
&=
operator like with the arithmetic operatorsSlide9
Typecasting and Concatenation Example
New Topics:
Option Strict
Option Explicit
Concatenation between
s
tring
Concatenation between a string and a number
Explicit typecasting to double
Explicit typecasting to stringSlide10
String Properties and Methods
You can get certain information about a string and perform some actions on it with some of VBs predefined properties and methods (Assume we have a string
str
)
str.Length
– returns how many characters are in the string
str.ToUpper
– returns all the characters in the string in upper-case
str.ToLower
– returns all the characters in the string in lower-case
str.Trim
– returns a string with all the spaces at the beginning and end removed
str.Substring
(param1, param2)
– returns a portion of the string starting at the character at
param1
to the character at
param2
Note: The numbering of the characters start at 0, so the first character is 0, the second is 1, and so on
str.IndexOf
(param1)
– returns the position of the first
occurance
of the substring
param1
, -1 if
param1
is not a substring of
str
Slide11
String Properties and Methods Example
New Topics:
Length
ToUpper
ToLower
Trim
Substring
IndexOfSlide12
Documentation
Comments
– are lines of code that are ignored by the compiler
Often used to write notes in the code to explain what is does
Comments in VB start with
‘
‘Here I am just declaring a variable
Dim
variableName
As Integer
It is good programming practice to have comments in your code to give an overview of what a section does
Good Examples
‘Here I compute the distance traveled
‘Below I take the user’s input
‘Now I output the answer to the results text box
Bad Examples
‘This code works but I don’t know why
‘More lines of code
‘Taken from the course webpageSlide13
File Comment Block
At the top of all of your project I want you to include the following information
Author’s Name
Course
Date Due
Brief Description
‘ Author: Eric Heim
‘ Course: CS0004
‘ Date Due: 2/2/2011
‘ Description: This code computes the distance
‘ traveled by a car by taking in the
‘ speed it was going and how long it
‘ was driving