Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String(n,c): does it work in VBS?

    I've been abused by the String() function in VBS.

    In a class, with a class variable int, calling <pre>result = String(int,".")</pre>

    just does not work! Changing the code to <pre>result = String(CInt(int),".")</pre>

    doesnt work. Setting another variable like this <pre>dim L
    L = CInt(int)
    result = String(L,".")</pre>

    doesnt work either.

    What am I overlooking?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: String(n,c): does it work in VBS?

    According to my book, the number is supposed to be a Long, but since VBS is not strongly typed, I find that hard to believe. This code, when run from a .vbs file on the desktop, works for both:

    Dim int, lng
    int = 5
    int = CInt(int)
    msgbox String(int,"X")
    lng = 10
    lng = CLng(lng)
    msgbox String(lng,"X")

    Is it possible that int has somehow become null when you get to the String function?

  3. #3
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String(n,c): does it work in VBS?

    I just tired a small class and it's working ~ there must be something else in the slightly larger class that i am not seeing.

  4. #4
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String(n,c): does it work in VBS?

    MISSPELLED VARIABLE!!! <img src=/S/barf.gif border=0 alt=barf width=64 height=23>

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: String(n,c): does it work in VBS?

    My sympathy. We've all been there and done that. Glad you found it!

    StuartR

  6. #6
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String(n,c): does it work in VBS?

    What irks me is that <html><%option explicit%> doesnt work as protection, but <%option explicit%> <html> does.

    The school of experience definitely has the highest tuition. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String(n,c): does it work in VBS?

    Hi Peter,

    I assume that the project you're currently working on will be presented in the ASP environment?

    I've learned a few tricks about optimizing Classic ASP code that I'll pass on, just in case you (or someone else) may find them helpful:

    (This may explain why the Option Explicit isn't working properly if it follows the <html> tag)
    As an ASP page is processed, the code inside the script blocks (<%..%>) is handled by a separate process from the standard HTML code. As it turns out, the page will process much faster with fewer script blocks - because the Script engine will have much less back-and-forth to do with IIS.

    One way you can help your code is by using the Response.Write() command for small blocks of HTML rather than closing and reopening the Script block just for a couple of markup tags. Of course, the extreme would be to build the whole page in Script syntax. I don't think it makes enough difference to make that worthwhile, but I was surprised how much faster things seemed to run by eliminating some of the script starts and stops... (I'd be curious to hear the actual time results if anyone knows of testing that's been done on this.)

    Which brings me to your Option Explicit issue. I wonder if the problem results from placing that declaration anywhere AFTER the page is passed to IIS (and then back to the Script engine). That might explain why it works properly when it comes before any markup. Just a thought...

    Also, I usually try to write as much Script before writing ANY markup. As mentioned above, there will usually be at least some script mixed in with HTML, but I try to keep it to a minimum...

    Hope this helps!

  8. #8
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String(n,c): does it work in VBS?

    Mark ~ Your points are well taken, as usual!

    In order to stay sane, I've tried to develop some rules for developing with VBS. So far, that means that my VBS script pages have this format:
    <pre><%option explicit%>
    #include virtual="filename", repeat as necessary
    <%
    class something
    ... etc
    end class
    %>
    <html> and the rest of the markup
    ... etc.
    <%
    Dim s
    set s = new something
    s.Run
    set s = nothing
    %>
    </body>
    </html>
    </pre>

    For pure classes, the files are simpler:
    <pre><%
    class something
    ... etc
    end class
    %>
    </pre>

    I strive to have 0 "script variables" and keep everything inside classes. Also note that altho VBS calls them classes, they're really just capsules.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •