2016-12-19 7 views
0

私が取り組んでいるプロジェクトのいくつかの簡単なクラスを作成しようとしています。私は循環参照の問題に遭遇しています、そして、私はここでの解決策が何であるか分かりません。私のクラスは間違って設計されている可能性があることを理解していますので、これを正しく行う方法に関する全般的な推奨事項に感謝します。参照クラス内の同じクラス

私は、このクラスとリファレンスをどのように構築しているかを示すために、コードを細かい部分まで細かく切り詰めました。

Public Class Utilities 
    Public Class Result 
     Public Success As Boolean = False 
    End Class 
End Class 

Public Class Customer 
    Public Class Contact 

     Public Class ContactList : Inherits Utilities.Result 
      Public Contact As Contact() 
     End Class 

     Public ID As String 
     Public Created As Date 
     Public CreatedBy As New Contact 

     Public Shared Function Search(oInput As Contact) As ContactList 

      Dim oOutput As New ContactList 

      ReDim oOutput.Contact(500) 

      While oDataReader.Read() 
       oOutput.Success = True 
       oOutput.Contact(i) = New Contact() 
       oOutput.Contact(i).ID   = oDataReader("ID").ToString() 
       oOutput.Contact(i).Created  = oDataReader("Created").ToString() 
       oOutput.Contact(i).CreatedBy.ID = oDataReader("CreatedByID").ToString() 
       i = i + 1 
      End While 
      oDataReader.Close() 
      ReDim Preserve oOutput.Contact(i-1) 

      Return oOutput  

     End Function   
End Class 

このコードを実行すると、エラーが発生します。「System.StackOverflowException」という例外がスローされました。

+3

は 'クラスContact'は、それ自体が新しい' Contact'を作成するメンバー 'CreatedBy'ています。新しい' Contact'これを作成し、メンバー 'CreatedBy'を持ってい... – Plutonix

+0

これは新しいインスタンスに初期化しなかった "きれいな"ものです。 –

+0

StackOverflowExceptionsは無限再帰によって発生します。デバッガ(ビジュアルスタジオ)を使用してコードをステップ実行すると、メソッドが無限ループで繰り返されることに気付くでしょう。それは犯人になるでしょう。あなたがすでにこれを観察しているなら、質問にその情報を掲示しないことに恥をかく。 – Igor

答えて

0

クラス内でクラスを作成する代わりに、すべてのクラスを個別に作成することができます。あなたはこのようなコードを持つことができます。

Public Class Utilities 
    Dim myResult as Result 

    Public Shared Function Search(oInput As Contact) As ContactList 

     Dim oOutput As New ContactList 

     ReDim oOutput.Contact(500) 

     While oDataReader.Read() 
      oOutput.Success = True 
      oOutput.Contact(i) = New Contact() 
      oOutput.Contact(i).ID   = oDataReader("ID").ToString() 
      oOutput.Contact(i).Created  = oDataReader("Created").ToString() 
      oOutput.Contact(i).CreatedBy.ID = oDataReader("CreatedByID").ToString() 
      i = i + 1 
     End While 
     oDataReader.Close() 
     ReDim Preserve oOutput.Contact(i-1) 
     'you can store the array to myResult here for further use. and to reduce the Search function usage. 

     Return oOutput  

    End Function   
End Class 
Public Class Result : Inherits ContactList 
     Dim Success As Boolean = False 
    End Class 

Public Class Customer 
    Dim myContact as Contact 
    Dim myContactList as ContactList 
End Class 

Public Class ContactList 
     Public Contact As Contact() 
End Class 

Public Class Contact 
    Public ID As String 
    Public Created As Date 
    Public CreatedBy As String 
    'CreatedBy should have Contact ID only as you are using the ID field of CreatedBy contact 
    'Where ever you are referencing the CreatedBy field you can have the ID string of the CreatedBy Contact 
    'On which you can lookup the data you needed. 

    Public Function setCreatedBy (_CreatedBy As String) 
    CreatedBy = _CreatedBy 
    End Function 
End Class 
関連する問題