Solved

VB script classes - load array on initialize

Posted on 2013-06-28
1
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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