Solved

VB script classes - load array on initialize

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

14 Experts available now in Live!

Get 1:1 Help Now