Forum Discussion

joe_2's avatar
joe_2
Contributor
10 years ago

Silly question of the day: Subroutines with subroutines

So I tried to do something and it didn't work, and I googled it and found it didn't work because vbscript can't do that...

I was trying to put a subroutine within a subroutine.

 

I have a script.  It's full of subroutines.

Each subroutine does a slightly different thing, being passed two variables.

One variable is always the name of a property of an object.  The other is the contents of that property.

Like so: 

Sub Mysub(propertyname,content)

      <some code that does something like clicking, or right clicking, etc..., as well as setting up some log messages, maybe doing some calculations to come up with a value to put in a global variable...>

End Sub

Sub NextSub(propertyname,content)

    <code for NextSub>

End Sub

The property name and content are usually being used with a findchild statement

 

 

I almost always use one of only a few properties, and I thought to set up a sub-sub for each property name I most commonly use, just to make the {main} scripts more readable. 

 

The syntax for using these subroutines is something like:

Call Scriptname.Mysub("propertyname", "Content")

I'd like to be able to use something like

Call Scriptname.Mysub.Propertyname("Content")

 

I can currently also use the syntax:

Mysub "propertyname", "content"

and I'd like to be able to:

Mysub.propertyname "Content"

 

But I can't figure out how to get there.  I think I'm googling the wrong things, because nothing i've seen looks like a solution.

I know when I was learning C++ I did stuff like this, but just can't figure it out in VBScript.  Probably because VBScript is so limited, comparatively.

  • If I understand your situation correctly, you can use Class-Sub instead of Sub-Sub. 

     

    Give it a try:

     

    In Unit1:

     

    Class ClsProperty
    Sub propertyName(strContent)
    msgbox strContent
    end Sub
    End Class

    Sub MySub(strContent)
    set obj = new ClsProperty
    obj.propertyName(strContent)
    End sub

     

    In Unit2:

     

    'USEUNIT Unit1

    sub Callee
    Unit1.MySub("test")
    End Sub

  • If I understand your situation correctly, you can use Class-Sub instead of Sub-Sub. 

     

    Give it a try:

     

    In Unit1:

     

    Class ClsProperty
    Sub propertyName(strContent)
    msgbox strContent
    end Sub
    End Class

    Sub MySub(strContent)
    set obj = new ClsProperty
    obj.propertyName(strContent)
    End sub

     

    In Unit2:

     

    'USEUNIT Unit1

    sub Callee
    Unit1.MySub("test")
    End Sub

    • joe_2's avatar
      joe_2
      Contributor

      Ah, yes.  It's been years since learning C++, and I had forgotten about classes.

      Time, I think, to access some VBScript tutorials on the web and re-learn about classes.

      Thank you for the point in the right direction.