withEvents变量'Label1'隐式定义'Label1',它与类'Form2'中的同名成员冲突

m.q*_*yum 1 vb.net

我想将数据从form1传递给form2,我使用了"The Properties Approach",如本文所示:http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

但我发生了以下错误 withEvents variable 'Label1' implicitly defines 'Label1', which conflicts with a member of the same name in class 'Form2'

Form1中

      Private ReadOnly Property _Label1() As String
        Get
            Return Label1.Text
        End Get
    End Property

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked

        Dim frm As Form2 = New Form2
        frm._Label1 = _Label1
        frm.Show()
    End Sub
Run Code Online (Sandbox Code Playgroud)

窗体2

   Friend WriteOnly Property _Label1() As String
    Set(ByVal Value As String)
        Label1.Text = Value
    End Set
End Property
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 6

朋友WriteOnly属性_Label1()As String

汇集错误消息,但问题是由您选择的名称引起的.该表单上有一个名为Label1的控件.Windows窗体设计器为它创建了一个声明,您可以在代码中使用Handles关键字,方便地为控件创建事件处理程序.该WITHEVENTS关键字让编译器自动生成该变量的一些代码.包含一个隐藏字段,它看起来像ildasm.exe:

.field private class [System.Windows.Forms]System.Windows.Forms.Label _Label1
.custom instance void [mscorlib]System.Runtime.CompilerServices.AccessedThroughPropertyAttribute::.ctor(string) = ( 01 00 06 4C 61 62 65 6C 31 00 00 )                // ...Label1..
Run Code Online (Sandbox Code Playgroud)

请注意编译器为该隐藏字段选择的名称.Yup _Label1,与您为您的财产选择的名称完全相同.KABOOM.

选择一个名称,任何名称,而不是以下划线开头的名称.