Title: Parametric Design using Digital Project
1Parametric Design using Digital Project
2VBScript?
- VBScript
- Microsoft Visual Basic Scripting Edition
- Started as a client-side scripting language to
create interactive webpages - Client-side?
- Operations on client in client-server environment
- Scripting language?
- Shorten edit-compile-link-run process
- No compiler, interpreter
- Batch, Unix shell, AutoLisp, VBA
3VBScript Tutorials
- Functions and Procedures
- Sub statement
- Function statement
- Data types and Variables
- Class
4Functions and Procedures
- Subroutine
- maintain and write programs by segregating
related code into smaller, manageable sections - reduce the number of lines of code
- Sub . . . End Sub Construct
- Function . . . End Function
5Sub . . . End Sub
- construct is used to define a subroutine that
is, a procedure that performs some operation but
does not return a value to its calling program.
Sub cmdButton1_OnClick MsgBox(Hello) End Sub
6Defining subroutines
- Automatically
- Constructor
- Ex) Class_Initialize
- Defining as an event handler
- executed automatically if it is defined as an
event handleras a routine that is executed
whenever some event occurs. - Referring to it by name
- executed at any time by referring to it by name
in another part of the script
7Subroutine names
- The name can contain any alphabetical or numeric
characters and the underscore character. - The name must start with a letter, not a numeric
character or underscore, and it cannot contain
embedded spaces. - The name cannot contain any spaces. Use the
underscore character to separate words to make
them easier to read. - The name cannot be a VBScript reserved word, such
as a VBScript statement. - Examples
- Sub 123MySub( ) ' Illegal
- Sub My Sub Routine( ) ' Illegal
- Sub MySub123( ) ' Legal
- Sub MySubRoutine( ) ' Legal
8Function . . . End
- construct is used to define a subroutine that
is, a procedure that performs some operation and
return a value to its calling program.
Function CubeIt(x) CubeIt x3 End Function
9Passing Variables
- allows us to write custom "black box" routines
that can behave differently depending on where
the routine has been called from and also on the
particular data values that the routine receives
from the calling program. - Passing by values
- Passing by references
10Passing Variables
- Sub AnotherSubRoutine( )
- some code. . . .
- mySubRoutine intvar1, strvar2, lngvar3
- more code that executes after mySubRoutine
- End Sub
- Sub mySubRoutine(intDataIn1, strDataIn2, _
- lngDataIn3)
- code which uses incoming data
- End Sub
11Passing by values
- Sub DoSubroutine( )
- Dim x
- x 10
- Response.Write "In DoSubroutine, x is " x
"ltPgt" - CallAnotherSub x
- Response.Write "Back in DoSubroutine, x is "
x - End Sub
- Sub CallAnotherSub(ByVal var1)
- var1 var12
- Response.Write "In CallAnotherSub, var1 is "
var1 - End Sub
12Passing by references
- Sub DoSubroutine( )
- Dim x
- x 10
- Response.Write "In DoSubroutine, x is " x
"ltPgt" - CallAnotherSub x
- Response.Write "Back in DoSubroutine, x is "
x - End Sub
- Sub CallAnotherSub(ByRef var1)
- var1 var12
- Response.Write "In CallAnotherSub, var1 is "
var1 - End Sub
13Exit statement
- Function functionname(argumentlist)
- . . . some calculation or manipulation
- If condition1 Then
- functionname result of calculation or
manipulation - Exit Function
- End If
- . . . perhaps some more code
- If condition2 Then
- functionname result of calculation or
manipulation - Exit Function
- End If
- End Function
14Variable and Data types
- Variable
- allows us to refer to the variable by its name,
rather than to focus on its implementation
details - is a placeholder or recognizable name for a
memory location - x 10
- y "Hello World"
- Constant
- keeps the same value throughout its lifetime
- cannot have its value changed throughout the life
of the program - Const myConstant 10
15Declaring Variable and Constants
- VBScript allows the implicit declaration of
variables - Recommends to use Dim
- Dim Variable_Name
16Data types
- VBScript has only a single data type, called a
variant - Variants contain many different types of data and
can automatically select the most appropriate
data type for the particular context in which it
is being used
17Types of Data in Variant
- Empty
- Null
- Boolean
- Byte
- Integer
- Long
- Single
- Double
- Date/Time
- Currency
- String
- Object
- Error
18Class
- Template for an object.
- Class...End Class construct
- Variable
- Properties
- Method
- Event
19Class Construct
- Class...End Class
- Class classname
- Class definition
- End Class
- Instantiation an object of a class
- Set objc new classname
20Class Variables
- Class has variables
- Dim varName1 , varName2...
- Private varName1 , varName2...
- Public varName1 , varName2...
- Dim, Public
- Open to outside
- Private
- Closed to outside
21Class Properties
- Access wrapper for private variables
- Property Get
- Property Let value
- Property Set reference
22Class Properties
- Class Computer
- Private modStrType
- Private oOS
- Public Property Let ComputerType(strType)
- modStrType strType
- End Property
- Public Property Get ComputerType( )
- ComputerType modStrType
- End Property
- Public Property Set OperatingSystem(oObj)
- Set oOS oObj
- End Property
- Public Property Get OperatingSystem( )
- Set OperatingSystem oOS
- End Property
- End Class
23Class Method
- Functions or subroutines in a class
- Class LaptopComputer
- Private modOwner
- Public Property Let CompOwner(strOwner)
- modOwner strOwner
- End Property
- Public Property Get CompOwner( )
- CompOwner modOwner
- End Property
- Public Function GetOwner( )
- GetOwner modOwner
- End Function
- End Class
24Class Event
- Two events are automatically associated with
every class you create - Class_Initialize
- is fired whenever you instantiate an object based
on this class - Class_Terminate
- is called when the script engine determines that
there are no remaining references on an object
25Statement
- A statement in a program is a basic sentence that
expresses a simple ideaits purpose is to give
the computer a basic instruction. Statements
define the types of data allowed, how data are to
be manipulated, and the ways that procedures and
functions work.
26Statements in VBScript
27IF Then Else Statement
- If condition Then statements Else elsestatements
- ' Or, you can use the block form syntax
- If condition Then
- statements
- ElseIf condition-n Then
- elseifstatements . . .
- Else elsestatements
- End If
- If A gt 10 Then A A 1 B B A C C B
- If A gt 10 Then
- A A 1
- B B A
- C C B
- End If
28Select Case Statement
- Select Case testexpression
- Case expressionlist-n
- statements-n . . .
- Case Else
- elsestatements-n
- End Select
- Dim Color, MyVar
- Sub ChangeBackground (Color)
- MyVar lcase (Color)
- Select Case MyVar
- Case "red" document.bgColor "red"
- Case "green" document.bgColor "green"
- Case "blue" document.bgColor "blue"
- Case Else MsgBox "pick another color"
- End Select
- End Sub
29For...Next Statement
- For counter start To end Step step
- statements
- Exit For
- statements Next
- For I 1 To 10
- For J 1 To 10
- For K 1 To 10
- . . .
- Next
- Next
- Next
30Do...Loop Statement
- Do While Until condition
- statements
- Exit Do
- statements
- Loop
- Do statements
- Exit Do
- statements
- Loop While Until condition
31Do...Loop Statement
- Do Until DefResp vbNo
- MyNum Int (6 Rnd 1)
- ' Generate a random integer between 1 and 6.
- DefResp MsgBox (MyNum " Do you want
another number?", vbYesNo) - Loop
- Dim Check, Counter
- Check True Counter 0 ' Initialize
variables. - Do ' Outer loop.
- Do While Counter lt 20 ' Inner loop.
- Counter Counter 1 ' Increment
Counter. - If Counter 10 Then ' If condition is
True... - Check False ' set value of flag
to False. - Exit Do ' Exit inner loop.
- End If
- Loop
- Loop Until Check False ' Exit outer loop
immediately.
32VBA Tutorials
- VBScript, VBA, VB
- Object Oriented Programming
- Data types
- Class
33VBA?
- Visual Basic for Application
- Hosted in other application
- Interpreted language
34What can you do with VBA?
- Creating instances of OLE (ActiveX) objects
within your code - Creating classes (reusable custom software
objects) - Linking to ODBC databases like Access and SQL
Server - Integrating with the messaging API (MAPI) to
create Exchange/Mail applications - Integrating with Internet and intranet solutions
- Creating custom dialog boxes and forms
- Storing and retrieving data from the Windows
registry - Detecting and handling errors
- Incorporating ActiveX controls into the
application interface - Passing data between VBA-enabled applications
with a minimum of programming and fuss - Driving a second VBA-enabled application from
within a first VBA-enabled Application - And more
35VBScript vs. VBA
- Weakly typed language vs. Typed
- Early binding
- Binding
- Association of values with identifier
- vs. Late binding
- Integrated Development Environment
36VBA vs. Visual Basic
- Hosted vs. Independent
- Interpreted vs. Complied
- Platform dependency
- Object?
- An instance of a Class
- An individual unit of run time data storage
- Data member, Method
37Object Oriented Programming
- OOP is all about programming using something
called an object - OOP consists of designing a bunch of classes most
of which will be used as templates to make
objects - The objects will be used to hold data and will be
initialized with constructors and perform useful
functions by being asked to run their methods
From CS1322 lecture slide
38What is an object?
- A chunk of memory which contains some data and
methods which can operate on the data. - Something that when created may have had special
code run to initialize it. - Something which has additional behavior defined
by methods which can - Be passed data via parameters
- Perform calculations/operations which may
- Change some of its data
- Perform some desired operation
- Return the value of some of its data
- Return the results of calculations using both
data passed in and data contained in the object
From CS1322 lecture slide
39How do we define different types of objects?
- For each different type of object we write a
class. - New classes may be based on existing classes to
allow code reuse - A class serves as a blueprint or template
defining how all objects made from this class
operate and what data they hold. - One class may be used to make one or many objects
From CS1322 lecture slide
40Classes and Objects
Class It describes the form of an object. It is
a template or blueprint. It specifies data
representation, behavior, and inheritance (via
variables, methods and parents)
Object It is an instance of a class. It has a
unique copy of every non-static variable.
(i.e., the instance variables but not
the class variables (those labeled with
static).
Difference between a class and an object of
that class is analogous to the difference
between a type and a variable of that type.
Naming Conventions Classes Identifiers begin
with cap letters for each word in
the identifier, e.g., class GraduateStudent Objec
ts Identifiers begins with lower case letter,
then caps for other words in
identifier, e.g.,
graduateStudentPresident
From CS1322 lecture slide
413 key features
- Abstract data types
- Encapsulation of data and behavior
- Classes as templates used to produce 1 or more
instances or objects - Inheritance
- Provides for code-reuse
- Polymorphism/Dynamic Binding
- Different objects in the same family may have
identically named methods - The language takes care of sorting out the
correct method to invoke for us
From CS1322 lecture slide
42Procedural vs. Object Oriented
- Procedural
- We create an array to store some data
- We write a procedure that can be passed an array
an it will sort it
- Object Oriented
- We create a class that contains an array AND a
routine that will sort that array - The data and the behavior (ability to sort) are
in one "container
From CS1322 lecture slide
43Data Types in VBA
- Boolean
- Byte
- Integer
- Long
- Single
- Double
- Date/Time
- Currency
- String
- Object
- User Defined type
- Variant
44Declaring Variables and Constants
- Dim VariableName As datatype
- Private VariableName As datatype
- Public VariableName As datatype
45Variable scope
- Private
- visible in the entire module, but nowhere else
- Friend
- visible in the entire project and thus has
project-level scope. - Public
- visible not only to the project in which it is
declared, but also to any external project that
holds a reference to the project
46Assessing variables
- Public Module Module1
- Public iModulePublic As Integer
- Friend iModuleFriend As Integer
- End Module
- Project1.Module1.iModulePublic 100
- Project1.Module1.iModuleFriend 100
47Array
- An array is a static structure. That is, once
declared, its size or number of elements may not
be changed - A major advantage of arrays is direct random
access to any element in O(1) time. - In general arrays are homogeneous, that is each
cell holds the same type of thing. This concept
will be "stretched" when we come to polymorphism.
48Array Declaration
- Dim myArray(5) As Integer
- myArray Array(12,3,13,64,245,75)
- Dim iDynamicArray() As Integer
- ReDim iDynamicArray(10)
49Multi Dimensional Array
- Dim arrayname(upperboundDimension1, _
- upperboundDimension2, ....) As Datatype
- Private myArray() As Integer
- Private Sub cmdButtonOne_OnClick
- ReDim myArray(10,5)
- End Sub
- Private Sub cmdButtonTwo_OnClick
- ReDim myArray(4,10,2)
- End Sub
50Class
- Properties
- Events
- Methods
51Enumerated Constants
- Public Enum empTypes
- empTypeOne 1
- empTypeTwo 2
- empTypeThree 3
- End Enum
- If iType empTypes.empTypeOne Then
- If iType empTypeOne Then
52Custom Event
- To report the progress of an asynchronous task
back to the client application from an
out-of-process ActiveX EXE component. - To pass through events fired by the underlying
control in an ActiveX custom control. - As a central part of a real-time multiuser
application in an n-tier client-server
application. (Incidentally, events cant be fired
from within a Microsoft Transaction Server
Context.) - To receive notification of events fired in
automation servers. - To query the user and receive further input.
53Usage
54Reference
- OReilly VBScript in a Nutshell, 2nd Edition