How to call a function from another form?

  • Thread starter Thread starter Nestor
  • Start date Start date
N

Nestor

Hello, I'm trying to call a function from another form.... If I have 3
forms, A, B and C. A calls form B and C, and I want to call a function in C
from form B. Is there anyway to do this?

Any advise will be appreciated.

Thanks.
 
Nestor said:
Hello, I'm trying to call a function from another form.... If I have 3
forms, A, B and C. A calls form B and C, and I want to call a function in
C from form B. Is there anyway to do this?

Any advise will be appreciated.

Thanks.

Make those functions public, create an instance of the form and call through
that instance.

Vivek
 
Nestor,

In order class memeber to be visible outside the class (without inhertance)
they have to be either public or internal. In the latter case the both
caller and callee needs to be compiler in the same assembly. Keep in mind
also that if you declare something public then the enclosing entity needs to
have the same or less restrictive declaration that is if you declare a
method internal the declaring type needs to be internal or public. If the
declaring type is declared nested in some other type the outer type needs to
have the right visibility also.
 
The problem is I don't want to create another instance of the other form C
from form B. Basically Form A created new instances of Form B and Form C. If
I were to create another new instance of Form C from Form B, I will end up
'popping' another new UI instance of Form C from C, meaning I will have 2
forms showing which is not what I wanted.

However I want to access some public functions of Form C from Form B. How do
I go about doing that? thru delegates and events?
 
When creating form B, you can pass an reference to form C to it, where
it will be held for later use. Will this do?
 
As Sericinus hunter points out, you can pass an instance of B to C - i.e. by
creating a public (or internal) property on C that B assigns itself (this)
to after creating B and prior to showing it. This can also be done as a
parameter to the ctor (of C) if you want, but be sure to leave the default
(parameterless) ctor, otherwise the designer can throw toys out of the pram.

*however*; I would first ask (of myself, so somewhat rhetorical) what the
nature of the method is; if they are highly specific and have a demanding
interface (i.e. by their nature requiring a number of highly specific
parameters) I might go down the above route. If, however, it is more about
notifications between the forms, then I would do this as an event on C (i.e.
CustomerSelected, PrintRequested, UniverseEnding etc), which B would
subscribe to prior to showing.

I would also check with myself whether the functions I want to call are
actually related to the UI; it might be that they exist better as public
functions of a helper class (i.e. FormatCustomerForDisplay, etc) that exist
in a separate class accessed (separately) from B and C.

All of which may-or-may-not help you pick a way forward...

Marc
 
Back
Top