Forum Discussion

finae's avatar
finae
Contributor
10 years ago

hiding private sub/functions

Hi all,

 

  Does anyone have an idea on how to hid sub/functions if its marked as private?



  i m using TC9





  script 1: 

  dim def

public hij

 private sub ABC

    log.message "test"

 end sub



script 2: 

  'USEUNIT SCRIPT1

  SCRIPT1.ABC   <--- you can see in the code completion even though its marked as private in script1



SCRIPT1.DEF <------ cannot see this in code completion since its private,

SCRIPT.HIJ    <----- can see this in code completion as its marked as public. 



 

so far the only way i can think of would be to create a class pull items into the class with only 1 function to create the instance of teh class



then in a secodn script to call those specific public routine ,and leave the others alone.





script1:

 class apple

    private name_

    private qty_



   private sub class_initialize

    name_=""

   qty_=0

   end sub

  public property Get Name

    Name=Name_

  end property

  public sub CreateApple(name,qty)

   name_=name

   qty_=qty

  end sub 

  private disposeApple()

   name_=""

   qty_=0

  end sub

end class

public function CreateAppleObj

   set CreateAppleObj=New Apple

end function



as all the functions/routine is not visible (to code completion)



script 2:

 'useunit script1

public apple



public function CreateApplObj

   set apple=script1.CreateAppleObj

end function



public function Name

   Name= apple.Name

end function



public function CreateApple(name,qty)

   apple.createapple name,qty

end function



problem with this is now i have 2 script to maintain.



any suggestion? 

thanks

3 Replies

  • Hi,

     

    I would also like to know about this.

     

    I would like to create a visible script with a list of functions that will be used in tests, and I would like to create a second (hidden) script that contains all of the many functions required to support the first script.

    These hidden functions would never be directly used in a test, so they should not be selectable in TestComplete list of available tests.

     

    Is there any way to achieve this in TestComplete?

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      No, this is not possible and it is my understanding that this is not a limitation of TestComplete but of VBScript (that can be solved via classes, as it was mentioned, but I am doubting that this will be convenient).

      A possible workaround is to introduce some prefix for the 'provate' functions (e.g. 'z_') so that they are grouped together in the code explorer panel and code completion window and reference the unit (via 'USEUNIT) with 'private' functions only from the unit that contains 'public' ones. In this case if someone tries to use the 'private' function from his/her code unit this will result in the runtime error (unless the reference to the unit with 'private' functions is added to the given unit, but this can be easily figured-out during the code review).

      • sbkeenan's avatar
        sbkeenan
        Frequent Contributor

        Hi

         

        I've done this in the past with VBScript and, as mentioned earlier, a class definition.

         

        If I understand the scenario, you want to have a project item that contains a number of public routines and functions that can be called from other project items.  However, this project item may also contain private routines/functions that the other project items should not be able to access.  So, within a class definition, declare those routines that you want to be able to use in other project items as public and those that you don't want to make available to the other project items should be made private.  For example, my first project item (Unit1) would look like this:

         

        Function createInstanceOfClass()
          Set createInstanceOfClass = New MyUsefulFunctions
        End Function
        
        class MyUsefulFunctions
        
        	public function usefulFunction(aNumber)
        		dim newNumber
        		newNumber = helperFunction(aNumber)
        		usefulFunction = newNumber
        	end function
        	
        	private function helperFunction(aNumber)
        		helperFunction = aNumber * 2
        	end function
        
        end class

         

        One of my other project items might look something like this:

         

        'USEUNIT Unit1
        
        sub myTest
        
        	dim classInstance
        	dim myNumber
        	
        	set classInstance = Unit1.createInstanceOfClass()
        	
        	'Test 1 - this should return a result of 6
        	myNumber = 3
        	myNumber = classInstance.usefulFunction(myNumber)
        	log.message("myNumber = " & myNumber)
        	
        	'Test 2 - this should fail because helperFunction isn't accessible
        	myNumber = 3
        	myNumber = classInstance.helperFunction(myNumber)
        	log.message("myNumber = " & myNumber)
        	
        end sub

         

        For the purposes of this example, the above project item calls both the public and the private routines from Unit1.  However, as the results below show, only the public function is accessible whilst a call to the private function results in an error:

         

        results.png

         

        It's a little cumbersome because of the way you have to instantiate the class within VBScript, not like JScript, however, I think it's a minor inconvenience.

         

        Hope you find it useful!!

         

        Regards

        Stephen.