2013-05-17 144 views
0

我有以下兩類我該如何解決

Public Class Square 
Private _ParentForm As Form 
     Public Sub New(ByVal Parent As Form) 
      _ParentForm = Parent 
     End Sub 

    'Implementation omitted 

End Class 


Public Class Board 
    Private _ParentForm As Form 

    Public Sub New(ByVal Parent As Form) 
     _ParentForm = Parent 
    End Sub 

    Private _Squares As List(Of Square) 

    Public Property Squares() As List(Of Square) 
     Get 
      Return _Squares 
     End Get 
     Set(ByVal value As List(Of Square)) 
      _Squares = value 
     End Set 
    End Property 

End Class 

此對象引用錯誤,當我使用followng代碼它給我對象引用不設置到對象錯誤

Dim b As New Board(Me) 
    Dim s As New Square(Me) 
    b.Squares.Add(s) 

的一個實例是什麼發生在這裏

回答

1

你沒有初始化_Squares領域。不記得究竟是如何做到這一點VB的,但應該是這樣的:

Private _Squares As New List(Of Square) 

達倫的答案是正確的,也不過與這個你不必擔心初始化類的外部列表。

1

您沒有Square的實例,b.Squares尚未初始化。你需要

b.Squares As New List(Of Square) 

然後,您可以添加b.Squares.Add(s)

+0

此語法不正確。它應該是'b.Squares =新的列表(廣場)' –