Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB script classes - load array on initialize

Posted on 2013-06-28
1
Medium Priority
?
257 Views
Last Modified: 2013-06-29
SAMPLE.sr is a text file of clear text XML documents delimited with >>>
How do I make sr.Runners populate during the initialization of the class?


<%
Class ScreenRunner

      Private m_runners

      Private Sub Class_Initialize
           m_runners =   GetScreenRunners()
      End Sub
      
      Public Property Get Runners()
            
      End Property
      
      Public Property Let Runners(m_runners)
      
           Runners = m_runners
      
      End Property
      
      
      

      Public Function GetScreenRunners()
      
            set oSR = Server.CreateObject("Scripting.FileSystemObject") :  Set oBlock = oSR.OpenTextFile(Server.MapPath("SAMPLE.sr"))

            Do While Not oBlock.AtEndOfStream
                  sBlock = sBlock & oBlock.ReadLine & vbcrlf
            Loop

      
            oBlock.close  : set oBlock = nothing :  set oSR = nothing


            if instr(sBlock, ">>>") > 0 then
                aBlocks = split(sBlock, ">>>")
            else
                dim aBlocks : ReDim aBlocks(1)  :  set aBlocks(0) = loadXStr(xStr)
            end if


            Dim xBlocks : ReDim xBlocks(ubound(aBlocks))

            for b = 0 to ubound(aBlocks)            
                  Set oXml = CreateObject("Microsoft.XMLDOM")
                  oXml.async = false
                  oXml.loadxml aBlocks(b)            
                  set xBlocks(b)  = oXml              
            next


      GetScreenRunners = xBlocks

      
      End Function


      
      
      Public Function sR(path)
      Set oSR = Server.CreateObject("Scripting.FileSystemObject") :  Set oBlock = oSR.OpenTextFile(Server.MapPath(path))
'



            Do While Not oBlock.AtEndOfStream
                  sBlock = sBlock & oBlock.ReadLine & vbcrlf
            Loop
            oBlock.close  : set oBlock = nothing :  set oSR = nothing



            if instr(sBlock, ">>>") > 0 then
                aBlocks = split(sBlock, ">>>")
            else
                dim aBlocks : ReDim aBlocks(1)  :  set aBlocks(0) = loadXStr(xStr)
            end if


      Dim xBlocks : ReDim xBlocks(ubound(aBlocks))

            for b = 0 to ubound(aBlocks)
            call loadXStr(aBlocks(b))
            set xBlocks(b)  = loadXStr(aBlocks(b))
               
            next
      sR = xBlocks
      End Function
      
      


End Class



      Dim sr
      Set sr = New ScreenRunner
         
      response.write sr.Runners(0)

%>
0
Comment
Question by:KeithMcElroy
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39286850
I think there are 2 problems:
- you need to return the private property in the Public Property Get
- you can't access the indexed values directly from the property outside the class (but you could make an indexed property I think)

The Let property needs to be the other way around but I'm not sure you need that so I commented it out for the moment.

Anyway, this works:
<%
Class ScreenRunner

      Private m_runners

      Private Sub Class_Initialize
           m_runners =   GetScreenRunners()
      End Sub
      
      Public Property Get Runners()
            Runners = m_runners
      End Property
      
'      Public Property Let Runners(m_runners)
'           Runners = m_runners
'      End Property
      
      
      

      Public Function GetScreenRunners()
      
            set oSR = Server.CreateObject("Scripting.FileSystemObject") :  Set oBlock = oSR.OpenTextFile(Server.MapPath("SAMPLE.sr"))

            Do While Not oBlock.AtEndOfStream
                  sBlock = sBlock & oBlock.ReadLine & vbcrlf
            Loop

      
            oBlock.close  : set oBlock = nothing :  set oSR = nothing


            if instr(sBlock, ">>>") > 0 then
                aBlocks = split(sBlock, ">>>")
            else
                dim aBlocks : ReDim aBlocks(1)  :  set aBlocks(0) = loadXStr(xStr)
            end if


            Dim xBlocks() : ReDim xBlocks(ubound(aBlocks))

            for b = 0 to ubound(aBlocks)            
                  Set oXml = CreateObject("Microsoft.XMLDOM")
                  oXml.async = false
                  oXml.loadxml aBlocks(b)            
                  set xBlocks(b)  = oXml               
            Next


      GetScreenRunners = xBlocks

      
      End Function


      
      
      Public Function sR(path)
      Set oSR = Server.CreateObject("Scripting.FileSystemObject") :  Set oBlock = oSR.OpenTextFile(Server.MapPath(path))
'



            Do While Not oBlock.AtEndOfStream
                  sBlock = sBlock & oBlock.ReadLine & vbcrlf
            Loop
            oBlock.close  : set oBlock = nothing :  set oSR = nothing



            if instr(sBlock, ">>>") > 0 then
                aBlocks = split(sBlock, ">>>")
            else
                dim aBlocks : ReDim aBlocks(1)  :  set aBlocks(0) = loadXStr(xStr)
            end if


      Dim xBlocks : ReDim xBlocks(ubound(aBlocks))

            for b = 0 to ubound(aBlocks)
            call loadXStr(aBlocks(b))
            set xBlocks(b)  = loadXStr(aBlocks(b))
               
            next
      sR = xBlocks
      End Function
      
      


End Class



      Dim sr
      Set sr = New ScreenRunner

      Dim srr, i
      srr = sr.Runners
      For i=0 To UBound(srr)
        response.write "<hr>"
        response.write Replace(Replace(srr(i).xml, "<", "&lt;"), vbCrLf, "<br>")
      Next

%>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question