在VB.NET中将对象作为参数传递

时间:2014-07-29 10:54:20

标签: vb.net

我是初学者,请告诉我如何将参数对象Person传递给Button.Click事件。我正在使用vb.net。这是我的代码:

Public Class Form1
    Public Class MyPersonClass
        Public Name As String
        Public Age As Integer
        Public Title As String
    End Class

    Public Sub DisplayPerson(ByVal person As MyPersonClass)
        Label1.Text = person.Name
        Label2.Text = person.Age.ToString()
        Label3.Text = person.Title
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



    End Sub
End Class

2 个答案:

答案 0 :(得分:3)

你没有 - Button1_Clickevent handler,你不应该手动调用它。当某个事件发生时,被称为,具有预定义的参数。你无法真正调整这些参数,因为它没有意义:事件将不再知道如何调用处理程序。

当然,您可以轻松编写自己的方法并将任何对象传递给它。你已经完成了DisplayPerson

答案 1 :(得分:1)

Private ExamplePerson As MyPerson  

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ExamplePerson = New MyPersonClass 'thanks Chris Dunaway for the correction
    ExamplePerson.Name = "Test Name"
    ExamplePerson.Age = 36
    ExamplePerson.Title = "Title Name"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DisplayPerson(ExamplePerson)
End Sub