我是初次使用类和OOP。 我创建了一个类,该类将在每次调用时创建一个新标签。 这是我的代码:

Public lbl As New Label
Public txt As New TextBox
Public controls As List(Of Control)
Public Sub New()
    'Add a label

    lbl.Name = "Label" & 1
    lbl.Text = "Student " & 1 & ":"
    lbl.Size = New Size(65, 20)
    lbl.Location = New Point(10, (10 * 22) + 5)

    controls.Add(lbl)
End Sub

当我调用此类时,我收到此错误消息:

An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe    Additional information: Object reference not set to an instance of an object.

消息突出显示的代码行是:

controls.Add(lbl)

任何帮助,将不胜感激,谢谢。

===============>>#1 票数:1 已采纳

Public lbl As New Label
Public txt As New TextBox
Public controls As List(Of Control)

Public Sub New()
  'Add a label

  lbl.Name = "Label" & 1
  lbl.Text = "Student " & 1 & ":"
  lbl.Size = New Size(65, 20)
  lbl.Location = New Point(10, (10 * 22) + 5)
  If (controls is nothing) = True Then controls = new list(of Control)
  controls.Add(lbl)
End Sub

  ask by Dan Hayes translate from so

未解决问题?本站智能推荐: