Reputation:
I have a panel2 inside a split container that has several user controls loaded into it. Panel 1 has an exit button and I want to call one of the sub routines that is in one of the user controls loaded into Panel2.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim dialogMessage As DialogResult
Dim a As New ucTimeTracker
dialogMessage = MessageBox.Show("Are you sure you want to exit?", "Exit Ready Office Assistant?", _
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If dialogMessage = Windows.Forms.DialogResult.Yes Then
ucTimeTracker.autoWriteFileOnExit()
Me.Close()
Else
Return
End If
End Sub
This line is giving me trouble.
ucTimeTracker.autoWriteFileOnExit()
I am getting (reference to non-shared member requires an object reference).
I want the exit button on frmMain.SplitContainer.Panel1 to call autoWriteFileOnExit() on the user control named ucTimeTracker that is loaded into splitContainer.Panel2
Upvotes: 0
Views: 2767
Reputation: 158319
You are using ucTimeTracker
to reference the method, which is the name of the class. Earlier in the method you create an instance of that class (Dim a As New ucTimeTracker
), so you should call a. autoWriteFileOnExit()
instead, if this is the instance that you want to use. If ucTimeTracker is a control on the form, you should instead use the name of that control.
To understand this you need to understand the difference between static members and instance members. A static member can be accessed directly through the class, without the need to create an instance of the class. In order to use an instance member you will need an instance of the class first. You can look at the Int32 class as an example:
' call a static method in the Int32 class, that returns an Int32 instance'
Dim asInt As Int32 = Int32.Parse("14")
' call an instance method on the Int32 instance, that will act on the data in '
' that instance, returning a string representation of its value '
Dim asString As String = asInt.ToString()
Typically static methods does not act on data that is held inside the class (though this is not always true), but rather acts on data passed to the methods through parameters. Instance methods have access to the internal data of that specific instance, and can act on that data (as in the example above).
Upvotes: 0
Reputation: 422026
It seems that you are using the user control class name ucTimeTracker
instead of the instance name. Click on the user control in design view and in the properties view, there's a "Name" property. use the value in the name property (probably ucTimeTracker1
) instead:
ucTimeTracker1.autoWriteFileOnExit()
Upvotes: 1