I have two projects. One is GUI, and the second is DLL.
I Call a function In DLL from Form1
The DLL function has to interface the form (Say, Change Back Color)
I would like to get the Calling form as a reference, without passing as parameter.
Here is Example from Form 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeBackColor()
End Sub
DLL function
Public Sub ChangeBackColor()
Dim CallingForm as Form
'''GET PARENT HERE'''
CallingForm.BackColor = Color.Cyan
End Sub
Obviously, I can do this if I pass Form every time, But trying to avoid that. Thank in advance
Someone Noted this question has been answered elsewhere. It has not been answered (or asked) as far as I could see. This is specific to Referencing a Form, not a method or other Object.
I have gotten the solution to reference the calling form, by referencing Form.ActiveForm And am now answering my own question below.
From a design perspective this is really a bad thing to want.
Designing your application this way has a number of serious problems:
ChangeBackColor
function is dependent on a form calling the method!! What if some other code block is calling this code? The code will fail!!The last point is especially a problem because this makes unit testing this piece of code extremely hard and error prone. Methods signatures are all about communicating the intend of the function and thereby also needs to tell which things (objects, values, services etc.) it needs to perform the work.
So if the form or whatever object is needed in the function show in it in the signature!!
If you don't like the fact that you need to add this in every call, you could prettify the code by creating it as an extension method:
which you call like this in your form instance:
I ended up calling the active form by using
The following Function works now (along with others)
Thank you everyone who tried helping