我已经创建了一个对象"电子邮件列表"其中包含"电子邮件"具有3个参数的对象(字符串地址,字符串主题,字符串主体)
然后我想通过创建" email"的更多实例来添加到列表中。但是,我已经尝试了很多不同的方法,而且没有任何方法。
Public Class Test
Public emails As List(Of Email)
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
emails(0).setAddress("Hello")
emails(0).setSubject("World2")
emails(0).setBody("Why don't you work?")
emails.Add(New Email("Hello2", "World2", "Why don't you work?2"))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = emails(0).getAddress
Label2.Text = emails(0).getSubject
Label3.Text = emails(0).getBody
Label4.Text = emails(1).getAddress
Label5.Text = emails(1).getSubject
Label6.Text = emails(1).getBody
End Sub
End Class
如果我点击button1,我会收到错误"对象引用未设置为对象的实例"。
感谢。
答案 0 :(得分:12)
您尚未创建列表实例,您只是声明了它。
Public emails As New List(Of Email)
' ^^^