Add a class function

Author Message
ebgreen

  • Total Posts : 8219
  • Scores: 98
  • Reward points : 0
  • Joined: 7/12/2005
  • Status: offline
Add a class function Friday, December 23, 2005 4:20 AM (permalink)
0
This is a function that will add a class from an external file. It is as close to a #include that I have been able to come up with in VBScript. The class that you want to add needs to be saved in a file and must be valid vbscript. Here is an example of its use (the example assumes that there is a valid class file in the same directory as the test code is running in and that the valid class file is named LoggerClass.vbs):

Option Explicit

Dim arrReturn

'This is an example of trying to load a class from a file that does not exist. This will fail.
arrReturn = AddClass("Fail.vbs")
WScript.Echo "Fail: " & arrReturn(0) & " - " & arrReturn(1)
'This line will load a class from the file LoggerClass.vbs as long as that file is present
arrReturn = AddClass("LoggerClass.vbs")
WScript.Echo "Success: " & arrReturn(0) & " - " & arrReturn(1)

This test code should produce this output:
Fail: 1 - The specified class file Fail.vbs does not exist
Success: 0 -


Here is the actual function:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ''    
  ''    Function AddClass(strClassFile)
  ''    Author:        ebgreen
  ''    Purpose:    Imports a class that is stored in an external file.
  ''    Takes:        strClassFile - The name of the file with the class To
  ''                    import. Must be a valid path.
  ''    Returns:    Array - (0)= The return code. 0 means success otherwise
  ''                                it will be the error code returned by
  ''                                the operation that failed.
  ''                        (1)= Any error message. Will be "" if success
  ''    Revision History:
  ''        12/23/05    RMD    Revision History Added
  ''
  ''    To Do:
  ''        Nothing at present
  ''
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Function AddClass(strClassFile)
      Dim oFSO, oFile, strFile
  
      Set oFSO = CreateObject ("Scripting.FileSystemObject")
      'Check to see if the file to be loaded exists. If it does not, return an error
      If Not oFSO.FileExists(strClassFile) Then
          AddClass = Array("1", "The specified class file " _
                      & strClassFile & " does not exist")
          Exit Function
      End If
      On Error Resume Next
      Err.Clear
      'OPen the file to be loaded. If the open fails, return an error.
      Set oFile = oFSO.OpenTextFile(strClassFile)
      If Err.Number <> 0 Then
          AddClass = Array(Err.Number, _
                      "The specified class file " & strClassFile _
                      & " could not be opened for reading.")
          Exit Function
      End If
      On Error GoTo 0
      'The file object is created so read all of the file and ececute it globally to load the class for use
      strFile = oFile.ReadAll
      oFile.close
      Set oFso = Nothing
      Set oFile = Nothing
      ExecuteGlobal strFile
      AddClass = Array("0", "")
  End Function



EDIT: Added comments
<message edited by ebgreen on Wednesday, January 04, 2006 4:27 AM>
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm
 
#1
    kirrilian

    • Total Posts : 629
    • Scores: 3
    • Reward points : 0
    • Joined: 3/15/2005
    • Location:
    • Status: offline
    RE: Add a class function Friday, December 23, 2005 2:39 PM (permalink)
    0
    could you please explain this in more detail or comment your code?


    Thanks!
    Have you searched [url="http://www.google.com"]here [/url]?
    [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
    [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
     
    #2
      ebgreen

      • Total Posts : 8219
      • Scores: 98
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      RE: Add a class function Wednesday, January 04, 2006 4:28 AM (permalink)
      0
      I added some comments. If it is still not clear enouh, let me know and I will explain further.
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        kirrilian

        • Total Posts : 629
        • Scores: 3
        • Reward points : 0
        • Joined: 3/15/2005
        • Location:
        • Status: offline
        RE: Add a class function Wednesday, January 04, 2006 4:40 AM (permalink)
        0
        so the executeglobal allows you to load code, but couldnt you just use any text file?
        eg. myClasses.inc rather than myClasses.vbs
        Have you searched [url="http://www.google.com"]here [/url]?
        [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
        [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
         
        #4
          ebgreen

          • Total Posts : 8219
          • Scores: 98
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          RE: Add a class function Wednesday, January 04, 2006 4:44 AM (permalink)
          0
          Yes you could use any file that is a text file containing valid VBScript code.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5
            kirrilian

            • Total Posts : 629
            • Scores: 3
            • Reward points : 0
            • Joined: 3/15/2005
            • Location:
            • Status: offline
            RE: Add a class function Wednesday, January 04, 2006 5:05 AM (permalink)
            0
            excellent, now i can REALLY obfuscate my code

            talk about job security!
            Have you searched [url="http://www.google.com"]here [/url]?
            [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
            [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
             
            #6
              ebgreen

              • Total Posts : 8219
              • Scores: 98
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              RE: Add a class function Wednesday, January 04, 2006 5:18 AM (permalink)
              0
              We use it to put a folder on all desktop machines that is in the path and has .vbs files containing all of our commonly used classes.
              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
               
              #7
                kirrilian

                • Total Posts : 629
                • Scores: 3
                • Reward points : 0
                • Joined: 3/15/2005
                • Location:
                • Status: offline
                RE: Add a class function Wednesday, January 04, 2006 5:18 AM (permalink)
                0
                ive just verified that you can load the code from any text file, very useful!
                Have you searched [url="http://www.google.com"]here [/url]?
                [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                 
                #8

                  Online Bookmarks Sharing: Share/Bookmark

                  Jump to:

                  Current active users

                  There are 0 members and 1 guests.

                  Icon Legend and Permission

                  • New Messages
                  • No New Messages
                  • Hot Topic w/ New Messages
                  • Hot Topic w/o New Messages
                  • Locked w/ New Messages
                  • Locked w/o New Messages
                  • Read Message
                  • Post New Thread
                  • Reply to message
                  • Post New Poll
                  • Submit Vote
                  • Post reward post
                  • Delete my own posts
                  • Delete my own threads
                  • Rate post

                  2000-2012 ASPPlayground.NET Forum Version 3.9