2
votes

I just want to know what is the difference between "Public function" and "Function"

if anyone can help , and that will be highly appreciated..

thanks

3

3 Answers

3
votes

In addition to the answer of Ekkehard.Horner, in QTP it is also possible to load Qtp Function Libraries (QFL) as .qfl or .vbs files.

A function, const or variable in a QFL that is private, can not be used in another QFL, Module or Action, while a public one can.

Functions, Constants and Variables are by default public:

' All public:
Dim MyVariable
Public MyOtherVariable
Const PI = 3.1415
Function GetHello
    GetHello = "Hello"
End Function
Sub SayHello
    MsgBox GetHello
End Sub

' All private:
Private myPrivates
Private Const HELLO = "HELLO!"
Private Function getHelloToo
    getHelloToo = HELLO
End Function
Private Sub sayHelloToo
    MsgBox getHelloToo
End Sub

Class Dog
    Public Function Bark
        Print "Bark! Bark! Bark!"
    End Function
End Class

Yes, classes are always private in a module. You have to return it from a function to make them public available:

' Placed in the same module as Class Dog
Public Function GiveMeADog
    Set GiveMeADog = new Dog
End Function
3
votes

The concept of Public vs. Private accessability is best explained by using a class:

Option Explicit

Class cPubPrivDemo
  Public   m_n1
  Dim      m_n2
  Private  m_n3
  Private Sub Class_Initialize()
    m_n1 = 1
    m_n2 = 2
    m_n3 = 3
  End Sub
  Sub s1()
    WScript.Echo "s1 (Public by default) called"
  End Sub
  Public Sub s2()
    WScript.Echo "s2 (Public by keyword) called"
  End Sub
  Private Sub s3()
    WScript.Echo "s3 (Private by keyword) called"
  End Sub
  Public Sub s4()
    WScript.Echo "(public) s4 can call (private) s3 from inside the class"
    s3
  End Sub
End Class

Dim oPPD : Set oPPD = New cPubPrivDemo

WScript.Echo "Can access public member variables of oPPD:", oPPD.m_n1, oPPD.m_n2

WScript.Echo "No access to oPPD's private parts:"
Dim n3
On Error Resume Next
n_3 = oPPD.m_n3
WScript.Echo Err.Description
On Error GoTo 0

WScript.Echo "Can call public subs:"
oPPD.s1
oPPD.s2

WScript.Echo "Can't call private sub .s3:"
On Error Resume Next
oPPD.s3
WScript.Echo Err.Description
On Error GoTo 0

WScript.Echo "private sub s3 can be called from inside the class:"
oPPD.s4

From the script's output:

Can access public member variables of oPPD: 1 2
No access to oPPD's private parts:
Object doesn't support this property or method
Can call public subs:
s1 (Public by default) called
s2 (Public by keyword) called
Can't call private sub .s3:
Object doesn't support this property or method
private sub s3 can be called from inside the class:
(public) s4 can call (private) s3 from inside the class
s3 (Private by keyword) called

you can see:

  1. A private component (variable, sub; the same holds for functions and properties) can be accessed from the inside of the component (here: class)
  2. A public component can be accessed from the outside (not shown but probably plausible: publics can be used from the inside too)
  3. Not specifying an access right/mode (?) explicitly (m_n2, s1) defaults to "Public"
  4. Short answer to your question: None - because of (3)

The VBScript Docs for "Public statement" say

Declares public variables and allocates storage space. Declares, in a Class block, a public variable.

and

Public statement variables are available to all procedures in all scripts.

So one could research/test whether and how the accessability rules apply to (combined) scripts (source code files). As I don't know anything about QTP's handling of multiple source files, I can't help you there.

2
votes

The issue of public and private would only matter when used within a class. Inside a VBScript class, the functions are public by default, therefore there would be no difference between the two. Using Private makes the function inaccessible from outside of the class.