Code Examples
  Home arrow Code Examples arrow Adding Methods to Custom Class Objects in ...
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? 
CODE EXAMPLES

Adding Methods to Custom Class Objects in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2009-03-17

    Table of Contents:
  • Adding Methods to Custom Class Objects in VBScript
  • Adding methods to a class
  • Completing the class
  • Implementing our new class

  • 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


    Adding Methods to Custom Class Objects in VBScript


    (Page 1 of 4 )

    In my last article I began showing you how to create your own custom class objects in VBScript. We started building the CompressedFolder class, a class that allows you to create and manipulate compressed folders natively. This article will expand on what you learned.

    We started by creating a class and adding various properties to it, but having a few properties doesn’t do much for this class.  We also need a way to create the compressed folder and to add to and remove files from it.  For that we need to add some methods.  These are the functions and subroutines that perform the magic when the class is called.

    Just like Properties, these methods can be either Public or Private.  Again, the Public methods will be exposed to scripts through the class object.  The structure of methods is identical to that of functions and subroutines used outside of a class, with the simple exception that we’ll be providing either a private or public declaration.

       Public Sub Create(strFile)

           If objFso.FileExists(strFile) Then objFso.DeleteFile(strFile)

           FullName = strFile

           NewCompressedFolder FullName

       End Sub

     

       Private Sub NewCompressedFolder(strPath)

           Const adTypeBinary = 1

           Const adTypeText = 2

           Const adWriteChar = 0

           Const adSaveCreateNotExist = 1

           Const adSaveCreateOverwrite = 2

     

           With CreateObject("ADODB.Stream")

              .Open

              .Type = adTypeText

              .WriteText ChrB(&h50) & ChrB(&h4B) & ChrB(&h5) & ChrB(&h6)

              For i = 1 To 18

                  .WriteText ChrB(&h0)

              Next

              .SaveToFile strPath, adSaveCreateNotExist

              .Close

              .Open

              .Type = adTypeBinary

              .LoadFromFile strPath

              .Position = 2

              arrBytes = .Read

              .Position = 0

              .SetEOS

              .Write arrBytes

              .SaveToFile strPath, adSaveCreateOverwrite

              .Close

           End With

       End Sub

    Here I’ve added two methods to my class.  The Open() method is a Public method that can be used by the class object in my scripts.  When I pass a parameter to this method, the class will internally call the private NewCompressedFolder() method.

    I could have easily housed all of the code in the NewCompressedFolder method within the Open method and eliminated the need for it.  I don’t like to do that.  I prefer to keep all of the internal workings of a class within its own private methods.  This is a pretty common practice for most programmers.

    More Code Examples Articles
    More By Nilpo


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek