2 different methods using same object methods?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
:rolleyes:
Sorry I wasn't sure what to write in question.

VB.NET:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim someproc As Process
        someproc = Process.Start("d:\vypress.msi")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'want this to done here.
        someproc.CloseMainWindow()

    End Sub
End Class

Simple?:(
 
Since you need the same reference you need to store the reference at a higher scope than the local method, some place that is available to both methods. That would be as a field in the class, so you can move the Dim variable declaration from the method to the class. This is also called module level, but I like class level better. When you declare a field in a class you also have to think about the access level for all those using that class, fields should in most cases be declared Private and not directly available to users of the class, so move the Dim... to class and replate Dim with Private.
 
but why don't we call constructor(new) for the process class.
That can be done, it has a constructor. Sometimes you'd do that, sometimes you'd use the Start function, perhaps in conjuction with a ProcessStartInfo instance.
 
Back
Top