-1

Form オブジェクトが引数付きで作成されている場合、フォーム値の入力中にエラーが発生するのはなぜですか?

エラーは次のとおりです。

非共有メンバーへの参照には、オブジェクト参照が必要です。

ファイル 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

ファイル ChildForm.vb

Public Class ChildForm

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

End Class
4

3 に答える 3

4

btnok のハンドラーでは、作成したインスタンスの名前ではなく、クラス名を使用しています。これでうまくいくはずです。

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
于 2013-01-23T20:29:56.167 に答える
1

ボタン クリック イベントで、ChildForm を oChildForm に変更します。

于 2013-01-23T20:29:15.570 に答える
0

コンストラクターの場合、値を定義する必要があります。次に例を示します。

Sub New() ' You can use "Overload" if it needs to be shared or no shared
          ' for a 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

使い方:

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

または:

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
于 2013-01-24T03:12:37.493 に答える