2016-12-19 3 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은 무한 재귀 때문에 발생합니다. 디버거 (예 : Visual Studio)를 사용하고 코드를 단계별로 실행하면 (실행되는대로) 무한 루프에서 메서드가 반복됩니다. 그것은 범인이 될 것입니다. 이미 이것을 관찰했다면 질문에 해당 정보를 게시하지 않은 것에 대해 수치스러워합니다. – 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 
관련 문제