开发者

withEvents variable 'Label1' implicitly defines 'Label1', which conflicts with a member of the same name in class 'Form2'

I want to pass data from form1 to form2 and i used "The Properties Approach" as shown in this article: http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

But i have following error occurd 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_LinkClic开发者_Go百科ked(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

Form2

   Friend WriteOnly Property _Label1() As String
    Set(ByVal Value As String)
        Label1.Text = Value
    End Set
End Property


Friend WriteOnly Property _Label1() As String

Confuzzling error message but the problem is induced by the name you picked. There's a control on that form with the name Label1. The Windows Forms designer created a declaration for it that you can use in your code with the Handles keyword, handy to create event handlers for the control. The WithEvents keyword makes the compiler auto-generate some code for that variable. Including a hidden field, it looks like this with 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..

Note the name that the compiler picked for that hidden field. Yup _Label1, exactly the same name you picked for your property. Kaboom.

Pick a name, any name, just not one that starts with an underscore.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜