Sharat Chandra Sharat Chandra - 3 months ago 7
Vb.net Question

Windows Form constructor does not accept arguments

Why do I get an error while filling the Form values, if the Form object was created with arguments?

The error is:


Reference to non-shared member needs an object reference.


File ParentForm.vb



Public Class Maincls
Dim oChildForm as New ChildForm("abc") ' Causes an error, but removing the arguments removes the error
Dim oChildForm as New ChildForm ' Does not throw an error
Public Sub btnok_click
ChildForm.tbXYZ.Text = "abc" ' Reference to non-shared member needs an object reference
End Sub

End Class


File ChildForm.vb



Public Class ChildForm

Public Sub New(tempString as String)
InitializeComponent()
End Sub

End Class

Answer

In the handler for btnok you are using the class name rather than the name of the instance you have created. This should do it.

Public Class Maincls

    Dim oChildForm as New ChildForm("abc") 'Causes Error, but removing the arguments removes the error
    Dim oChildForm as New ChildForm 'Does not thow error

    Public Sub btnok_click
        oChildForm.tbXYZ.Text = "abc"    'Reference to non-shared member needs an object reference
    End Sub

End Class
Comments