VBScript 11
VBScript 11
Session 11
Dani Vainstein
Dani Vainstein
OOP Model
Abstractions Encapsulation Polymorphism Inheritance Reusabillity
Dani Vainstein
Dani Vainstein
OOP Model Encapsulation The ability for the program to hide information about the implementation of a module from its users, i.e. the ability to prevent users from breaking the invariants of the program. The term encapsulation refer to Information hiding in software.
Dani Vainstein
Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name. Polymorphism allows a method of a class to be called without regard to what specific implementation it provides.
Dani Vainstein
Dani Vainstein
OOP Model Inheritance Defining classes as extensions of existing classes. In computer science, the term inheritance may be applied to a variety of situations in which certain characteristics are passed on from one context to another. The term originates with the biological concept of a parent passing on certain traits to a child
Dani Vainstein 9
Dimensions Flanks
Shape
Area
Perimeter
Radious *pi
Circle
angles Triangle
Height Width
Rectangle
Volume
3D
depth
Dani Vainstein
10
Dani Vainstein
11
Object Model
Object. Collection. Container object. Method. Event. Property. Attribute.
Dani Vainstein
12
Dani Vainstein
13
An object that contains other objects from different classes. Collection can have properties and/or methods.
Dani Vainstein
15
Dani Vainstein
16
Dani Vainstein
17
Dani Vainstein
18
Dani Vainstein
19
Dani Vainstein
20
Dani Vainstein
21
COM IUnknown
The fundamental COM interface, which must be extended by every valid COM interface. IUnknown exposes three methods:
QueryInterface, used to find out if the object supports a certain interface and return the interface if it does. AddRef, to increment the interface reference count. Release, to decrement the interface reference count.
Dani Vainstein
22
An implementation of one or more COM interfaces. COM objects are instances of COM classes.
Dani Vainstein
23
A pointer to a vtable pointer where the first three methods in that table are QueryInterface, AddRef, and Release.
Dani Vainstein
24
CORBA Common Object Request Broker Architecture RMI, JavaBeans SUN Microsystems technologies. RPC Remote Procedure Call.
Dani Vainstein 25
Set statement
Assigns an object reference to a variable or property, or associates a procedure reference with an event. To be valid, object must be an object type consistent with the object being assigned to it. The Dim, Private, Public, or ReDim statements only declare a variable that refers to an object. No actual object is referred to until you use the Set statement to assign a specific object. Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because these variables are references to (rather than copies of) the object, any change in the object is reflected in all variables that refer to it.
Dani Vainstein
27
Nothing Keyword
The Nothing keyword in VBScript is used to disassociate an object variable from any actual object. Use the Set statement to assign Nothing to an object variable. For example:
Set MyObject = Nothing
Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.
Dani Vainstein
28
CreateObject Function
CreateObject(servername.typename [, location]) Creates and returns a reference to an Automation object. servername - Required. The name of the application providing the object. typename - Required. The type or class of the object to create. location - Optional. The name of the network server where the object is to be created. Automation servers provide at least one type of object. For example, a word-processing application may provide an application object, a document object, and a toolbar object.
Dani Vainstein
29
CreateObject Function
For example, a word-processing application may provide an application object, a document object, and a toolbar object. To create an Automation object, assign the object returned by CreateObject to an object variable:
Dim ExcelSheet Set ExcelSheet = CreateObject("Excel.Sheet")
Dani Vainstein
30
CreateObject Function
Once an object is created, refer to it in code using the object variable you defined. you can access properties and methods of the new object using the object variable.
Creating an object on a remote server can only be accomplished when Internet security is turned off. You can create an object on a remote networked computer by passing the name of the computer to the servername argument of CreateObject. That name is the same as the machine name portion of a share name. For a network share named "\\myserver\public", the servername is "myserver". In addition, you can specify servername using DNS format or an IP address.
Dani Vainstein
31
Type Library
COM Class
Dani Vainstein
32
Dani Vainstein
33
New Keyword
Keyword used to create a new instance of a class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can only be used to create an instance of a class. Using the New keyword allows you to concurrently create an instance of a class and assign it to an object reference variable. The variable to which the instance of the class is being assigned must already have been declared with the Dim (or equivalent) statement.
Dani Vainstein
34
Dani Vainstein
35
Dani Vainstein
36
Dani Vainstein
37
Dani Vainstein
38
Provides a list of the drives attached to the system, either physically or logically. The Drives collection includes all drives, regardless of type. Removable-media drives need not have media inserted for them to appear in this collection.
Dani Vainstein
39
Contains methods and properties that allow you to create, delete, or move a file. Also allows you to query the system for a file name, path, and various other properties.
Dani Vainstein
40
Dani Vainstein
41
Contains methods and properties that allow you to create, delete, or move folders. Also allows you to query the system for folder names, paths, and various other properties.
Dani Vainstein
42
Dani Vainstein
43
Dani Vainstein
44
Use the CreateObject method to create a FileSystemObject object. Use the appropriate method on the newly created object. Access the object's properties.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Scripting is the name of the type library and FileSystemObject is the name of the object that you want to create.
Dani Vainstein 45
Do not use the "get" methods for newly created objects, since the "create" functions already return a handle to that object. For example, if you create a new folder using the CreateFolder method, don't use the GetFolder method to access its properties, such as Name, Path, Size, and so forth. Just set a variable to the CreateFolder function to gain a handle to the newly created folder, then access its properties, methods, and events.
Dani Vainstein
46
Dani Vainstein
47
Dani Vainstein
48
Dani Vainstein
49
Dani Vainstein
50
Dani Vainstein
51
Dani Vainstein
52
Creating Files
There are three ways to create an empty text file. The first way is to use the CreateTextFile method. The second way to create a text file is to use the OpenTextFile method of the FileSystemObject object with the ForWriting flag set. A third way to create a text file is to use the OpenAsTextStream method with the ForWriting flag set.
Dani Vainstein 53
Reading Files
To read data from a text file, use the Read, ReadLine, or ReadAll method of the TextStream object. If you use the Read or ReadLine method and want to skip to a particular portion of data, use the Skip or SkipLine method. The resulting text of the read methods is stored in a string which can be displayed in a control, parsed by string functions (such as Left, Right, and Mid), concatenated, and so forth.
Dani Vainstein 55
The FSO object model has two methods each for moving, copying, and deleting files
Move a file - File.Move or FileSystemObject.MoveFile Copy a file - File.Copy or FileSystemObject.CopyFile Delete a file - File.Delete or FileSystemObject.DeleteFile
Dani Vainstein 56
Lab 11.1
Tip
Const Const Const Const Const Const DRV_UNKNOWN = 0 DRV_REMOVABLE = 1 DRV_FIXED = 2 DRV_NETWORK = 3 DRV_CDROM = 4 DRV_RAMDISK = 5
Dani Vainstein
57
Lab 11.2
Declare the follow constants : Constants returned by Drive.DriveType
Const Const Const Const Const Const Const Const Const Const Const Const Const Const conDriveTypeRemovable = 1 conDriveTypeFixed = 2 conDriveTypeNetwork = 3 conDriveTypeCDROM = 4 conDriveTypeRAMDisk = 5
Dani Vainstein
58
Lab 11.2
Write the following functions. ShowDriveType(objDrive) - Generates a string describing the drive type of a given Drive object. ShowFileAttr(objFile) - Generates a string describing the attributes of a file or folder. GenerateDriveInformation(objFSO) reports about the Drive Letter,Path, Type, IsReady, ShareName, VolumeName, TotalSize, FreeSpace, AvailableSpace, and SerialNumber. GenerateFileInformation(objFile) File Name, Type, File Attributes, Date Created, Last Accessed, Last Modified and Size GenerateFolderInformation(objFolder) Folder Name, Folder Attributes, Date Created, Last Accessed, Last Modified and Size
Dani Vainstein
59
Lab 11.1
Create a Folder in the D:\ drive, if not exist in C:\ Drive. The name of the folder is VBSInfo. Create a file in the folder, TestInfo.txt The file will contain the report of the program. The data will displayed like a table (rows and columns with headers) use the vbTab for separate the data. For getting information about the directories and files, please DONT do it an all drives, select only the drive were you created your file.
Dani Vainstein
60
Lab 11.1
Create a Folder in the D:\ drive, if not exist in C:\ Drive. The name of the folder is VBSInfo. Create a file in the folder, TestInfo.txt The file will contain the report of the program. The data will displayed like a table (rows and columns with headers) use the vbTab for separate the data. For getting information about the directories and files, please DONT do it an all drives, select only the drive were you created your file.
Dani Vainstein
61
Whats Next
What means error handling. When to use On Error statements. Using the error object. Raising our own errors.
Dani Vainstein
62
63