Referencing an instance of a Form object

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi Folks,

Here is an issue im not getting my head around, a simple scenario I have 3 classes 2 are forms and 1 patient class.
FrmClients, FrmDialog Patient class

I create an instance of FrmClients called frmClient and during its operation a user click on a button to open an instance of FrmDialog called frmDialog

so far so good.

FrmClient has a publc method called UpdateDataGrid.

in the instance of frmDialog I want to call frmClient.UpdateDataGrid() but I get the error object not know.

Yet I fI create an instance if Patient called patient1 and it has a public method UpdateData it will allow me to refernce patient1.UpdateData()

What am I missing here and how can I call a ref to an instance publc methods, that i know exist.

kind regards
 
If you FrmDialog object wants to be able to make a FrmClients object do something, it has to know that it exists. If the FrmClients object creates the FrmDialog object and displays it, does the FrmDialog have any inherent way to refer to that FrmClients object? No, it doesn't. If someone gives you a phone, can you magically get the phone to make something happen in factory it was made in? Of course not. The factory would have to put something in the phone about itself so the phone could refer back to the factory. The same goes here. When the FrmClients object creates the FrmDialog object, it would have to pass a reference to itself into that FrmDialog object so that the FrmDialog object could refer back to it at a later time. Basically, the FrmDialog object needs a field of type FrmClients in which it can store a reference to its creator and the FrmClients object that creates it needs to pass that reference in, i.e. pass in Me.

That said, VB.NET provides an easier way to do this. If you're only using one instance of a form type at a time then you can use the default instance, which you access via the class name. For instance, instead of creating an instance explicitly and showing it like this:
Dim f2 As New Form2

f2.Show()
you can display the default instance like this:
Form2.Show()
You can then refer to that same instance else where in code without having to pass an instance around, e.g.
Form2.DoSomething()
Having said all that, default instances are a feature intended for beginners and those converting VB6 code. Experienced VB.NET developers consider them a bit of a scourge that create as many problems as they solve. In this particular case, the correct way to handle this is not the way you're trying to do it. The proper way would be for FrmDialog to raise an event and for the FrmClients that creates an instance to handle that event and call its own UpdateDataGrid method. That's if you even need to update the grid at all before the FrmDialog instance closes. If not then you can simply call the method immediately after ShowDialog returns or the FormClosed event is raised.
 
You don't need an example. Forms are objects just like anything else and you can already pass a reference to an object from one to another. You've assigned to variables and properties and passed arguments to methods haven't you? As I already told you in my previous post, for an object to refer to itself you use Me, so you already know how to assign the current object to a variable or property or pass it as an argument to a method.
 
OK, so lets assume we have 2 forms and a Client classes and an instance of each frmClient, frmDialog and objClient.

I want frmDialog to call a public method in object frmClient so class FrmDialog has to have a data member of type FrmClient
VB.NET:
Public Class FrmDialog
...
Public frmClient As New FrmClient
...
End Class
Now in my instance of frmClient that creates frmDialog I pass a reference of the creating object, frmClient into this instance!

VB.NET:
Dim frmDialog As New FrmDialog
frmDialog.frmClient = Me
frmDialog.Show   '...or .ShowDialog()...

The same must be true for objClient, when I create an instance I must pass it in turn to each other object that will want to use it
So if in object frmClient I create object objClietn and I want to pass to frmDialog I need to...

VB.NET:
Dim newClient As New Client
newCleint.setID = ....
newClient.setName = "...."
.
.
.
frmDialog.objClient = Me.newClient

Also class FrmDialog needs to know about Client so must have a member
VB.NET:
Dim objClient As New Client

Am I on the right track here or am I missing something?

kind regards
 
Last edited:
Back
Top