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. Error is Reference to non-shared member needs an object reference.

ParentForm.vb

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
ChildForm.tbXYZ.Text = "abc"    'Reference to non-shared member needs an object reference
End Sub

End Class

ChildForm.vb

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

End Class

Answers


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

In the button click event, change ChildForm to oChildForm.


For Constructor your must define value, For Example :

 Sub New()' you can use Overload if need shared or no shared 
    ' for non shared member

 End Sub

Public Class ChildForm
Private valStr As String

Public Sub New(ByVal str As String)
    Me.valStr = str ' Shared Memeber
End Sub
Public Property Text As String
    Get
        Return valStr
    End Get
    Set(ByVal value As String)
        valStr = value
    End Set
End Property
End Class

How To Use :

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim a As New ChildForm("Contoh")
    MsgBox(a.Text)
End Sub

or

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim a As New ChildForm
    a.Text = "Test"
    MsgBox(a.Text)
End Sub

Need Your Help

BackgroundWorker deadlock when using Control.Invoke in DoWork together with ShowDialog

c# winforms backgroundworker deadlock invoke

I'm running a long task in a BackgroundWorker, updating the UI via ReportProgress. However, in the middle of the process, I need to make a COM-call to get some data, and I think I need to do it on ...

Installing Java3D on Eclipse

java eclipse 3d install java-3d

I'm trying to use Java3D in my project. This is the error I receive:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.