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

    Converting VBA For Each to VBScript (Any or None)

    In VBA it is common to do this:

    Dim obj as TypeOfObject
    For each obj in CollectionOfObjects
    ' Do Stuff
    Next
    If not (obj is Nothing) then set obj = Nothing

    In VBScript, you would do this --

    Dim obj 'can't choose your own type!
    For each obj in CollectionOfObjects
    ' Do Stuff
    Next
    If not (obj is Nothing) then set obj = Nothing

    -- but that last line causes an error because the variant obj is not cast as an object. That's weird; everything cast explicitly using Set var = is cast as an object.

    Another thing that's annoyingly different. In VBA, to create a dynamic array of strings using Split, you do this:

    Dim strArray() as String
    strArray() = Split(blah blah blah)

    but in VBScript, you omit the parentheses!

    Dim strArray 'can't choose your own type!
    strArray = Split(blah blah blah)

    And people wonder why a "5 minute" conversion takes half an hour to figure out... Anyway, maybe this will help someone doing a conversion in the future, either with a Windows Script Host, Internet Explorer, or Classic ASP project.

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Converting VBA For Each to VBScript (Any or None)

    As a side note: I consider myself a "good boy" in the set-your-objects-to-Nothing department, but I don't do it with objects that have been set only by a For Each loop. I'd be interested to hear if anyone has ever encountered a VB failure to automatically reset one of those variables to Nothing. (Based on a little experimentation, it seems to happen immediately after the loop finishes, rather than at the end of the procedure -- although I realize that doesn't necessarily mean VB couldn't screw it up now and then.)

    As a side note to my side note, VB guru Mark Hurst calls the idea that VB is somewhat unreliable about automatically cleaning up after object variables an "urban myth". If any of the Lounge gurus have strong feelings on this subject (one way or the other), I hereby invite them to weigh in.

Posting Permissions

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