Solved

VB script classes - load array on initialize

Posted on 2013-06-28
1
233 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now