Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Understanding Objects
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Understanding Objects
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2008-02-12

    Table of Contents:
  • Understanding Objects
  • What objects provide
  • How COM objects are built
  • More about objects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Understanding Objects - What objects provide


    (Page 2 of 4 )

    Objects typically offer three types of data or code to a script.  They may use any or all of these three types.  They include:

    • Methods – These are code procedures that perform specific tasks.  They are generally in the form of subroutines and functions.
    • Properties – These are in a sense variables.  They represent a stored value.  This value may be retrieved or set dependent upon whether or not the property is read-only.
    • Events – These are special hook and sink strategies that are used to notify other code when a specific event has taken place.  Code can be written to react to these events.

    These data types must be exposed explicitly by the class in order to be used publicly.  Let’s take a look at the Shell Automation Service object as an example.  This is a COM object that allows you to interact with the Windows shell.  Before you can use it, it must be referenced, or instantiated, in your code.  VBScript and Jscript each do this differently.

    VBScript:

    Set objShell = CreateObject("Shell.Application")

    Jscript:

    var ShellObject = new ActiveXObject("Shell.Application");

    VBScript’s CreateObject function and Jscript’s ActiveXObject function both return an object reference that must be assigned a variable name.  You indicate which object you would like to instantiate by specifying its namespace.  This is a unique identifier that follows the COM naming convention.

    Objects may also contain sub-objects.

    Once an object has been instantiated, you can use its reference to access any exposed properties and methods.  In OOP, or object-oriented programming, this is done with dot syntax.  The remaining examples in this article will be written in VBScript.

    Set objShell = CreateObject("Shell.Application")

    objShell.MinimizeAll

    In the example above we’re calling the MinimizeAll method of the Shell object to minimize all open windows.  Notice that dot syntax is being used: object.method.

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set colDrives = objFso.Drives

    intNum = colDrives.Count

    In this example, we’re using the FileSystemObject’s Drives property to return a collection of drives.  This is a sub-object of the FileSystemObject.  We then use its Count property to return the number of sub-objects that it contains.

    In VBScript and WSH, some objects do not require instantiation.  They are made available by the environment.  Two examples are the WScript object, which references the currently active WSH session, and VBScript’s Err object, which references the Standard Error output stream.

    Because these objects are made available by the environment, they can be accessed immediately without first connecting to them or assigning them to variables.  You can see both in use in the following example.

    WScript.Echo WScript.FullName

     

    If Err.number <> 0 Then

       WScript.Echo "There was an error."

     

    End If

    Notice that there was no need to include any Set statements.  Instead, we are able to begin using properties and methods from these objects immediately.

    More Windows Scripting Articles
    More By Nilpo


     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT