how to reference an object that already exists

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

Hello,

I am trying to reference an object that already exists,
ie, I have a dll project (dllProj) which contains a simple
form. Then I have a main app and a secondary app which
both have references to dllProj. When I start the
project, the main app starts up. I then invoke the
dllProj from the main app and pass data to a property in
dllProj (PropertyOne) which then displays in a textbox on
dllProj. I can set data to another property in dllProj
(NameProperty) and then I can read NameProperty in the
main app. Then the main app starts up a secondary app
(form). I would like to read the value of NameProperty in
dllProj from the secondary app. I tried the following in
the secondary app but got an error:

Dim frm As dllProj.dllFormClass
Private Sub Form_Load(...)
Dim s As String
s = frm.NameProperty
msgbox s
End Sub

I get the error that the object frm was not set to an
instance of dllProj.dllFormClass. In the main app I say

Dim frm New dllProj.dllFormClass
Dim s As String
s = frm.NamProperty

This works fine except it is a new instance. I would like
to access the existing content of NameProperty contained
in dllProj from the secondary app. So, in addition to
having a reference to dllProj in the secondary app I also
tried

Imports dllProjNamespace

this did not help either. Is there a way I can reference
the already existing instance of dllProj from my secondary
app? Do I need to use something like GetObject?

Thanks,
Rich
 
You must pass an instance of your existing form (or object), into the
new form (or object).

One way to do this would be to overload the constructor of the second
class to take the argument. For example (watch for typos):

'Button click in Form1 creates Form2 and passes ref to Form1
Public Sub Button1_Click(...) handles button1.click
Dim frm As New Form2(Me) 'Note that I'm passing in Me
frm.ShowDialog()
End Sub


'Now in Form2, we overload the constructor:
Public Sub New(frm As Form1)
'Do something with frm here
frm.NameProperty = "whatever"
End Sub

There are other ways, but this is a simple way.
 
Thanks. Actually, the 2nd form is a second application.
I invoke it with System.Diagnostics.Process.Start
(shelling to it). So it is not part of the original
application, although, it IS part of the project, 3
projects in one. The original application, the dll
application, and the 2nd application. I already tried the
constructor idea, but Process.Start will only pass a
string argument and not an object. Thus, I was thinking
about some form of GetObject.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top