title: VB.Net Test if Object Exists
body: <p>I have a pretty simple VS 2013  Windows Forms Application which is made up of 3 .vb pages.<br>
  Note - I 'inherited' this application and did not develop it myself.  </p>
  
  <p>On one page an Application Object is created.</p>
  
  <pre><code>Dim oVFP As Object
  oVFP = CreateObject("VisualFoxPro.Application")
  
  ' Set up application object settings
  oVFP.DoCmd("SET EXCLUSIVE OFF")
  &lt;... and so on ...?  
  </code></pre>
  
  <p>On the User GUI Form I have an EXIT button whose Click method is on one of the other .VB pages.<br>
  This EXIT may be clicked at any time whether the oVFP object is 'working' or not.  </p>
  
  <p>Since the user might click on the Exit button while the oVFP is running, I'd like the btnExit Click method to check if oVFP exists and, if so, terminate it.  </p>
  
  <p>I tried:  </p>
  
  <pre><code>Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  IF NOT oVFP is Nothing then
    oVFP.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oVFP)
    oVFP = Nothing
  ENDIF
  </code></pre>
  
  <p>but since the oVFP is created on the other .vb page, it is not defined on the .vb code page with the btnExit method.  </p>
  
  <p>How can I, on the btnExit click method, test for the application object  oVFP  and, if it exists, terminate it.</p>
  
  <p>Your assistance/advice would be gratefully appreciated.  </p>
  
  <p>Thanks</p>