1

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.

Ess Kay
  • 598
  • 4
  • 20
  • Why would you need to call a DLL method from a Form to change the Form's backcolor? – Ňɏssa Pøngjǣrdenlarp Sep 22 '17 at 13:38
  • **Short answer:** No. **Long answer:** Possible duplicate of [IS there any way to get a reference to the calling object in c#?](https://stackoverflow.com/questions/420541/is-there-any-way-to-get-a-reference-to-the-calling-object-in-c) – Visual Vincent Sep 22 '17 at 13:57
  • @Plutonix That was just an example. the real code is much bigger and has nothing to do with color, but the reference principle is the same – Ess Kay Sep 22 '17 at 18:00
  • but if you must know.. The DLL takes a screenshot of the application where its called from. I need to get the form to find its size and location, so that screenshot is not full screen. This dll will be used in other applications, so its separate from the application – Ess Kay Sep 22 '17 at 18:05

2 Answers2

2

From a design perspective this is really a bad thing to want.

Designing your application this way has a number of serious problems:

  • You're relying on something like an ambient context, which is a anti pattern if used incorrectly. And this is a typical case of incorrect use because of:
  • you're hiding the fact that 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:

Public Module FormExtensions
   <Extension>
   Public Sub ChangeBackColor(form as Form, newBackColor as Color)
        form.Backcolor = newBackColor
   End Sub
End Module

which you call like this in your form instance:

Me.ChangeBackColor(Color.Red)
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
  • The backcolor was just example to illustrate the point of what i want to achieve, not the actual problem. I really actually need this to get Calling form's Location and size and also set it as a parent of the new form in the DLL. Also, i don't care about good practices in this case, i just need this working – Ess Kay Sep 25 '17 at 17:55
  • Essentially, I want to have a function like FOO() which then references the parent's common properties (size, location, itself) . This way I can overlay the DLL form on the same screen as the calling form – Ess Kay Sep 25 '17 at 17:59
1

I ended up calling the active form by using

Form.ActiveForm

The following Function works now (along with others)

Public Sub ChangeBackColor()
  Dim CallingForm as Form
  '''GET PARENT HERE'''
  CallingForm = Form.ActiveForm
  CallingForm.BackColor = Color.Cyan
End Sub

Thank you everyone who tried helping

Ess Kay
  • 598
  • 4
  • 20