passing objects by reference to a sub 
Author Message
 passing objects by reference to a sub

I have a script that uses the Exec method of the WshShell
object and parses StdOut. The thing is, StdOut can give
differently formatted text streams and for each format I
need a different parser. What I want to do is to create a
subroutine/function to which I can pass the WshScriptExec
object and a filehandle so the subroutine/function can
parse StdOut of that object and also write to the file
that I pass as an argument. Kind of:

Set WshShell = CreateObject("WScript.Shell")
Set oFSO_Out = CreateObject("Scripting.FileSystemObject")
Set oStream_Out = oFSO_out.CreateTextFile("output.out")
Set oExec=WshShell.Exec("logtool.exe")

Sub MySub(oStream_Out, oExec)

// Would the code below be correct?

Sub MySub(ByRef stream, ByRef exec)
....Do Until exec.StdOut.AtEndOfStream
.......stream.WriteLine(exec.StdOut.ReadLine)
....Loop
End Sub

Also can you create objects within a sub? Such as Regular
Expression objects?

Thanks.



Thu, 26 May 2005 03:31:30 GMT  
 passing objects by reference to a sub

Quote:

> I have a script that uses the Exec method of the WshShell
> object and parses StdOut. The thing is, StdOut can give
> differently formatted text streams and for each format I
> need a different parser. What I want to do is to create a
> subroutine/function to which I can pass the WshScriptExec
> object and a filehandle so the subroutine/function can
> parse StdOut of that object and also write to the file
> that I pass as an argument. Kind of:

> Set WshShell = CreateObject("WScript.Shell")
> Set oFSO_Out = CreateObject("Scripting.FileSystemObject")
> Set oStream_Out = oFSO_out.CreateTextFile("output.out")
> Set oExec=WshShell.Exec("logtool.exe")

> Sub MySub(oStream_Out, oExec)

> // Would the code below be correct?

> Sub MySub(ByRef stream, ByRef exec)
> ....Do Until exec.StdOut.AtEndOfStream
> .......stream.WriteLine(exec.StdOut.ReadLine)
> ....Loop
> End Sub

Yes, that should work...

Quote:

> Also can you create objects within a sub? Such as Regular
> Expression objects?

Yes, you can...

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 26 May 2005 04:40:24 GMT  
 passing objects by reference to a sub
First, there is an excellent post by Eric Lippert from a few years back covering
some of the quirks in ByVal/ByRef which you will want to have as at least a
reference; it's messy.  I have a copy on my site:

http://dev.remotenetworktechnology.com/wsh/eric_lippert_parens_in_pro...

ByVal/ByRef does not matter for the example you posted, but in any case
explicitly specifying ByRef is no guarantee something will be called that way;
issuing a ByVal call as outlined by Eric will make a "copy" of a variable as it
passes it to the Sub - and even though you may be specifying ByRef in the sub
arguments, you will only affect the temporary global copy which is thrown away.

Other stuff inline...


Quote:
> I have a script that uses the Exec method of the WshShell
> object and parses StdOut. The thing is, StdOut can give
> differently formatted text streams and for each format I
> need a different parser. What I want to do is to create a
> subroutine/function to which I can pass the WshScriptExec
> object and a filehandle so the subroutine/function can
> parse StdOut of that object and also write to the file
> that I pass as an argument. Kind of:

The nice thing here is that a variable is a variable.  You can pass anything as
long as you do it correctly; you MAY run into problems if you try passing a
subobject rather than the original parent in some cases.  The simplest way to
segment code that I have is to write a function that does all the processing and
configuration of object details and then passes back the object reference.

Quote:

> // Would the code below be correct?

Sure; this works perfectly.  HOWEVER - there is no need for you to worry about
this.  Even if you were passing flat variables instead of object references, you
are not doing anything to either value which needs to be passed back to the
global code.

Here's an example calling the code byref:

    Set WshShell = CreateObject("WScript.Shell")
    Set oFSO_Out = CreateObject("Scripting.FileSystemObject")
    Set oStream_Out = oFSO_out.CreateTextFile("c:\tmp\output.out")
    Set oExec=WshShell.Exec("%COMSPEC% /c dir")

    Call MySub(oStream_Out, oExec)

    Sub MySub(ByRef stream, ByRef exec)
         Do Until exec.StdOut.AtEndOfStream
              stream.WriteLine(exec.StdOut.ReadLine)
         Loop
    End Sub

If you call it ByVal, using the following syntax for the Call statement and
leaving out the  ByRef keywords:

    Call MySub((oStream_Out), (oExec))

you will see that the exact same results occur.

Quote:
> Also can you create objects within a sub? Such as Regular
> Expression objects?

Sure, but a function is the best thing to use IMO.  Here's a trivial example:

    set rx = MakeARegex

    WScript.echo TypeName(rx)

    Function MakeARegex
        set MakeARegex = new RegExp
    End Function

======================

If you have a set of variables which you need to share globally, you can
explicitly set their scope to public to start with, and then you don't need to
pass them.  For example, let's take your example:

Public oStream_Out, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oFSO_Out = CreateObject("Scripting.FileSystemObject")
Set oStream_Out = oFSO_out.CreateTextFile("c:\tmp\output.out")
Set oExec=WshShell.Exec("%COMSPEC% /c dir")

MySub

Sub MySub
 Do Until oExec.StdOut.AtEndOfStream
  oStream_Out.WriteLine(oExec.StdOut.ReadLine)
 Loop
End Sub

This is particularly handy if you do a bunch of file processing for example, and
isolate everything in procedures.  You can create an instance of FSO with a
public scope, and not have to deal with either all of the overhead of recreating
it in every call to the procedure, or with passing it as an argument explicitly.



Thu, 26 May 2005 04:50:06 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing an object reference by reference where the object has a default property

2. Sub(V1): passed by value or by reference?

3. Sub(V1): passed by value or by reference?

4. Passing object references to the functions in setInterval

5. Passing values by reference from java / vb script to ActiveX object

6. Passing a variable by reference in a COM object

7. Passing objects by reference

8. Passing Object References from Application to Script Code

9. Pass a reference to a COM object

10. Passing values by reference from java / vb script to ActiveX object

11. Passing arguments by reference to automation object

12. Passing COM objects by reference

 

 
Powered by phpBB® Forum Software