240 likes | 321 Views
CN2180 Chapter 4. Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP. Agenda. Constants, Variables, Arrays, and Dictionaries. The Story of Captain Adventure Assignment Quiz. How data is defined. Three ways to define or declare variable Const Dim Redim. Constant.
E N D
CN2180Chapter 4 Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP
Agenda • Constants, Variables, Arrays, and Dictionaries. • The Story of Captain Adventure • Assignment • Quiz
How data is defined • Three ways to define or declare variable • Const • Dim • Redim
Constant • User defined • Built-in • vbTab • vbCrlf
Assigning Data to Constants [public | private] Const ConstName = Expression • See Table 4.3 on Page 84 • See MsgFormatter.vbs on Page 84
VBScript Data Types • There are only one type called a variant • Boolean • Date • String • See table 4.4 on Page 90
Defining Variables • Dim variable name • Assign a value • Option Explicit • See BigBadWolf.vbs on Page 92
Variable Naming Rules • Less than 256 characters long • Must begin with an alphabetic character • Only letters, numbers, and _ allowed • Reserved words cannot be used • Space is not allowed • Must be unique
Hungarian Notation • Add three characters in front of variable name • Date = dtm • Error = err • Object = obj • String = str • See Page 95
Modifying Variable Values • Dim strMyName • strMyName = “Kemtis” • strMyName = “Kunanuraksapong
Arithmetic Operations • See MathDemo.vbs on Page 97
Order of Precedence • ^ - Exponential • - - Negation • * - Multiplication • / - Division • \ - Integer division • Mod - Modulus • + - Addition • - - Subtraction
Environment Variables • What is environment variables? • TEMP • TMP • See ComputerAnalyzer.vbs on Page 101
A collections of related data • Array • Index • Value Dim ArrayName(dimensions) Dim ArrayName(dimensions,dimensions)
Processing Array Contents For Each element In group Statements…. Next [Element] See ArrayDemo.vbs on Page 106
Getting a handle on the size • Ubound() • Lbound()
Resizing Arrays • Dim astrMyArray () • RedimastrMyArray (10) • Redim Preserve astrMyArray(20) • See ResizeArray.vbs on Page 110
Erasing Array • Erase ArrayName
Dictionaries • Just like Array • Define own pair of key and value • Three properties • Count (Return number of object) • Item (Get or Add an item) • Key (Add new key)
Dictionaries (Cont.) Dictionaries method • Add (Add key/item) • Exists (Boolean / True if exist) • Items (Return all Items) • Keys (Return keys) • Remove (Delete a key/item pair) • Remove All (Remove all key/item pairs)
Work with Dictionary Dim dictObject Set dictObject = CreateObject(“Scripting.Dictionary”) dictObject.Add “Model1555”,”Red Bike” dictObject.Add “Model1888”,”Blue Bike” dictObject.Item(“Model1999”) = “Green Bike” If dictObject.Exists(“Model1555”) = true then _ msgbox “Model 1555 is a “ & dictObject.Item(“Model1555”) dictObject.Remove “Model1555” dictObject.removeall
Parsing Arguments • Must run with cscript.exe cscript displayargs.vbs tic tac toe cscript displayargs.vbs tic “super tac” toe • See ArgumentProcessor.vbs on Page 116
Captain Adventure game • Beginning the captain Adventure script • Setting up Constants and Variables • Creating a splash Screen • Collecting User Input • Assembling and Displaying the story
Assignment • Challenge #1 Add 2 more user input and adding more story to the story line. • Challenge #2 Use an Array to store the user input collected instead.