Function

  • Thread starter Thread starter PEter
  • Start date Start date
P

PEter

Hello.
I have declared function in form1
For example
Public Function name()
.......
end Function.


And how can I call This function From FORM2.
Thanks
 
delcare a reference to Form1 in Form2... call method

however this takes up more memory than you probably should, I would suggest
putting it in its own common class that both can reference.

-CJ
 
* "PEter said:
I have declared function in form1
For example
Public Function name()
......
end Function.

Functions always return values, if they don't return values, declare
them as 'Sub' instead of 'Function'.
And how can I call This function From FORM2.

You need a reference to the instance of the other form. If you are
instantiating 'Form1' from within 'Form2', you have a reference:

\\\
Private m_MyForm1 As Form1
..
..
..
m_MyForm1 = New Form1()
m_MyForm1.Show()
..
..
..
m_MyForm1.DoSomething()
///
 
Firstly, you need to have a variable in the class form2 that references
form1. This can be achieved by passing form1 through the New parameter or
via a function/property as follows:

''' this code for form2

Public m_Form1 As Form1 = Nothing

Public Sub New(ByRef MyFirstForm as Form1)
m_Form1 = MyFirstForm
End Sub

Private Sub SomeFunction()
''' call my function on form1
m_Form1.name()
End Sub

Hope this helps,
Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
www.callofcthulhu.com www.deadlandsgame.com
-
 
Hi Peter,

You can do that, however it will give you know benefit.
In your form2 you can than do
dim frm1 as new form1
frm1.name
And then you have a new instance from a form1 with which you can do nothing.

Better is to do you actions from your form1 (when your form2 is a
dialogform) or to create a shared class which holds some shared variables
(this what was in the past a module, however a shared class makes all much
more compact and tells where it is. Keep in mind that a it is of course all
the time static in your program)

I hope this helps?

Cor
 
Back
Top