Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a Private Sub from another fmr

Status
Not open for further replies.

michelleDub

Technical User
Jun 3, 2003
16
IE
Hi
I want to call a Private Sub from another frm. I'm pretty new to VB. I'm wondering if there is anything in VB like friend functions in C++ or if there is any other way around this problem.
Thanks in Advance
Michelle
 
As far as I know you can't call a Private Sub from outside the module/form it's declared in. If you could your Private Sub would hardly be, er, Private.

If the Sub is Public then you could.


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
If you want to call private sub from another form, is probably because you have there an code which works into your actual form as well. Than my advice will be:

Try to make a public function and this code will be available to your entire application like:

Public Function YourFunction (MyVar1 as integer, MyVar2 as integer) as integer
' do something with myvar1 and myvar2
' than
YourFunction = myvar1 + myvar2
end function

MyVar1 and MyVar 2 is not necessary, it was an example. This function will sum the both values and shall give the result.
Let's say you want to calculation inside a textcontrol:

Code:
textbox1 = YourFunction(5,8)

So you see if you have a public function you can write less code for your app.

Hope this helped a bit.
 
Thanks, Do u mean to call the private sub from the public function in the form it is defined in.
thanks
 
Hi MicheleDub

You can declare as Friend (Just as in C++)
I'm pasting a copy of MSDN documentation on this declaration:


Friend


Modifies the definition of aprocedure in a form module orclass module to make the procedure callable from modules that are outside theclass, but part of theproject within which the class is defined. Friend procedures cannot be used in standard modules.

Syntax

[Private | Friend | Public] [Static] [Sub | Function | Property] procedurename

The required procedurename is the name of the procedure to be made visible throughout the project, but not visible to controllers of the class.

Remarks

Public procedures in a class can be called from anywhere, even by controllers of instances of the class. Declaring a procedure Private prevents controllers of the object from calling the procedure, but also prevents the procedure from being called from within the project in which the class itself is defined. Friend makes the procedure visible throughout the project, but not to a controller of an instance of the object. Friend can appear only in form modules and class modules, and can only modify procedure names, notvariables or types. Procedures in a class can access the Friend procedures of all other classes in a project. Friend procedures don't appear in thetype library of their class. A Friend procedure can't be late bound.


Regards,
Carlos
 
What I am saying is this:

Put your code you want to use it in many form inside a public function and after that call it from whereever you want.
 
Hey, so how would one actually go about calling a friend function from another forms module?

-Jedi420
 
A private sub by definition is a sub ONLY accessible by the containing module...

This allows you to have functions/subs of the same name in other modules within a single project...

If you want to call it from another module, you are elliminating the functionallity of the Private keyword...

You can use Public in place of Private then Call it like so...

---In Form1---
Public Function TestIt(X as Integer) as Integer
...
TestIt = Answer
End Function

---In Form2---
...
Results = Form1.TestIt(X)
--------------

If not by just saying...
Results = TestIt(X)



Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top