Please help - How to call functions that exists in the main application. The call should be initiate

  • Thread starter Thread starter Anand Ganesh
  • Start date Start date
A

Anand Ganesh

Hi All,

I need some help. I am sort of not sure how to approach this problem.

I have a MAINPROGRAM. This is the core application.

I have asked two of my staff to developed two different Windows Controls.
Say ControlA.dll and ControlB.dll.

As usual I know how to add these .dlls as the reference and then start using
these in my MAINPROGRAM.

Now my question is,

ControlA.dll has a button. If that button is clicked ControlA.dll should
call a function in my MAINPROGRAM and pass on a arraylist to the
MAINPROGRAM.

Similarly ControlB.dll has a button. If that button is clicked ControlB.dll
should call a function in my MAINPROGRAM and pass an integer and a float
value to the MAINPROGRAM.

How will I make these .dlls call one of the existing functions in my
MAINPROGRAM. Do I have to pass my MAINPROGRAM pointer to these .dlls?

In other words how will a component call the functions in the main
application from which the component is implemented.

Thanks
Anand Ganesh
 
That would create what are called circular dependencies which you don't
want.

You can try Delegates. Have your main program register a callback (via
events) that act on Button Clicks in ControlA.dll and ControlB.dll.

-vJ
 
Hi Vijaye,

Thanks for the real good suggestion.

I am going to look into these keywords like callback , events and delegates.

Is there any article which deals with this type of scenarios?

Thanks
Anand Ganesh
 
Hi Vijaye,

It looks like I can use Interface.

But can you please give me some hints on how will I use interfaces in this
situation?

Thanks
Anand Ganesh
 
Using interfaces means, in your case, you would need another dll that
provides all the interfaces while the existing dlls can provide
implementations.

Here's an example:

Dll 1: Provides Interface, IDocument

Dll A: references Dll1, provides ButtonA whose onClick event will call
IDocument.DoSomething()

Dll B: references Dll1, provides ButtonB whose onClick event will call
IDocument.DoSomething()

MainEXE: Implements IDocument, uses Dll1, DllA and DllB. It can pass its
reference to ControlA and ControlB, which can call back on DoSomething.

However, with your current state of implementation, delegates would be
easiest.

In your ControlA and ControlB, add a new event of type EventHandler.

For example

public class ControlA : Control
{
public event EventHandler DoSomethingCallback;

protected override void OnButtonAClick(object sender, EventArgs e)
{
if (DoSomethingCallback != null)
DoSomethingCallback(this, e); // You can send in an EventArgs
derivative
}
}

Do the same for ControlB.

In your MainApplication, where you're using ControlA, do something like
this...

ControlA myControlA;
myControlA.DoSomethingCallback += new EventHandler(DoSomething);

And then provide an implementation for DoSomething

private void DoSomething(object sender, EventArgs e)
{
// Do Something
}

Hope this helps

-vJ
 
Back
Top