Announcement

Collapse
No announcement yet.

How to use an INCLUDE statement within a Function using ASP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to use an INCLUDE statement within a Function using ASP

    I have an ASP file (functions.asp) that holds a series of functions I call from several different .asp pages that display information to the user (display1.asp, display2.asp, etc). I include functions.asp at the top of each displayX.asp page which needs to use those functions, and it works fine.

    Several of these functions contain long blocks of text that are identical in each function, and also need to be updated from time to time, so I'd like to put these long blocks of text into a single file and INCLUDE it within the functions. But I cannot figure out how to do this.

    Here's an example of how these files are structured - here's the display1.asp file:

    Code:
    <!-- #include virtual="functions.asp" -->
    
    <%
    
    'does three calculations and displays the results to the user
    
    response.write "The result of the first calculation is " & calculation1("Bathroom light") & "<br>"
    response.write "The result of the second calculation is " & calculation2("Bathroom light") & "<br>"
    response.write "The result of the third calculation is " & calculation3("Bathroom light") & "<br>"
    
    %>
    and here is the functions.asp file:

    Code:
    <%
    function calculation1(deviceName)[INDENT] [/INDENT][INDENT]'do a calculation and return the value
    
    dim data(3)
    data(1) = 10
    data(2) = 20
    data(3) = 30
    
    calculation1 = data(1) + data(2) + data(3)[/INDENT]
     
    end function
    
    function calculation2(deviceName)[INDENT]'do a calculation and return the value
    
    dim data(3)
    data(1) = 10
    data(2) = 20
    data(3) = 30
    
    calculation2 = (data(1)*2) + (data(2)*5) + (data(3)*10)[/INDENT]
     
    end function
    
    function calculation3(deviceName)[INDENT]'do a calculation and return the value
    
    dim data(3)
    data(1) = 10
    data(2) = 20
    data(3) = 30
    
    calculation3 = (data(1) /20) + (data(2)/40) + (data(3)/60)[/INDENT]
     end function
    These are nonsensical made-up examples, obviously, but they correctly show the structure of my files. What I want to do is to take the common fixed text:
    Code:
    dim data(3)
    data(1) = 10
    data(2) = 20
    data(3) = 30
    and place it into a file called "commonDataArray.asp" and then include this into each of the three functions. But I can't figure out if this is possible, and if it is how to format the INCLUDE statement within each function.

    Anyone know how to do this? Your help would be much appreciated.

    #2
    To my knowledge, you cannot nest INCLUDE statements; they need to be defined at the top of the called page.

    So your display1.asp code should look something like:

    Code:
    <!-- #include virtual="commonDataArray.asp" -->
    <!-- #include virtual="functions.asp" -->
    
    
    <%
    'does three calculations and displays the results to the user
    
    response.write "The result of the first calculation is " & calculation1("Bathroom light") & "<br>"
    response.write "The result of the second calculation is " & calculation2("Bathroom light") & "<br>"
    response.write "The result of the third calculation is " & calculation3("Bathroom light") & "<br>"
    %>
    functions.asp:

    Code:
    <%
    function calculation1(deviceName)
    'do a calculation and return the value
    
    calculation1 = data(1) + data(2) + data(3)
    
    end function
    
    function calculation2(deviceName)
    'do a calculation and return the value
    
    calculation2 = (data(1)*2) + (data(2)*5) + (data(3)*10)
    
    end function
    
    function calculation3(deviceName)
    'do a calculation and return the value
    
    calculation3 = (data(1) /20) + (data(2)/40) + (data(3)/60)
    
    end function
    %>

    CommonDataArray.asp:

    Code:
    <%
    dim data(3)
    data(1) = 10
    data(2) = 20
    data(3) = 30
    %>
    The alternative is not to use an INCLUDE and read data from an ini file.
    Jon

    Comment


      #3
      Sorry for the delay in replying - that works perfectly, thanks so much. Now that I see the solution I'm not sure why I couldn't figure that out myself, but that's the benefit of the discussion forums.

      Thanks again.

      Comment

      Working...
      X