Forum Discussion

blearyeye's avatar
blearyeye
Contributor
14 years ago

Useunit and folders

I'd like to organize my scripts and would like to use folders to help. Under Script in the project explorer tree I can add a folder called, say, "Some Scripts". I can include some scripts in that folder. However, I can't seem to reference them from other scripts using Useunit. What am I missing?



--Bill
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The folders in the project explorer are simply for visual organization.  They don't specify any referencing ability in script code itself.
  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi Bill



    As well as using USEUNIT at the beginning of your script(s), you also need to add a reference to the global scrips that you wish to use.



    For example, if you have a script called globalScript, held in a folder called Global, then if you are creating a test in folder TestGroup called test1, then at the beginning of test1 you should have the following line:



    'USEUNIT globalScript



    This should be the very first line of your script and should not have any spaces before it.



    The next thing you need to do is to right-click on the testGroup folder and select Add >> Existing Item.  Browse to the Global folder and select the globalScript file.



    You will notice that this places what looks like a duplicate copy of that script into your TestGroup folder.  This is not a duplicate file, but a reference to the actual file, which is still held in the Global folder.  You can see this by making a change to the file in one location then opening up the file from the other location - the change you made should be visible (providing you have saved it!)



    You should now be able to reference the script in the global folder.



    I use this technique whereby I have defined a class in a global location and reference it from my other scripts.  One more thing that you may need to be aware of is that in order to instantiate the class from your scripts, you must create a global function that can be called from your script that essentially creates a new instance of the class for use in your script.



    My global file looks something like this:



    Option Explicit



    Function DevXCtrl

        Set DevXCtrl = New clsDevXctrls

    End Function



    Class clsDevXctrls

    ....

    End Class



    The above file is saved as clsDevX so, in order to create a new instance of the class in my testing script, I use the following:



    'USEUNIT clsDevX

    Option Explicit



    Dim devX

    Set devX = clsDevX.devXCtrl

    ...



    This means that I can call any of the functions defined in the clsDevX class by using:

     

    Call devX.<function name>



     

    Hope this helps.



    Regards

    Stephen.
  • Thanks. Both replies were useful, especially Stephen's. It also pointed me in the direction of understanding classes in Javascript (my preferred language for TC).



    --Bill