circular reference


C

Ciccio

Hi all,

I have a question about circular references and how to avoid it.

Please let me explain you quickly my problem with a short example.

I have two separate modules: payroll and timesheet. Each one of those
modules contains different classes. No to see through my problem i try to
simplify it

In the timesheet module:

Option Strict On
Imports PayrollModule

Public Class ReferenceTestClass

Private m_propwithin As String
Public Property PropWithin() As String
Get
Return m_propwithin
End Get
Set(ByVal value As String)
m_propwithin = value
End Set
End Property

Private m_collectionwithin As New
ReferenceTestClassCollection
Public Property CollectionWithin() As
ReferenceTestClassCollection
Get
Return m_collectionwithin
End Get
Set(ByVal value As ReferenceTestClassCollection)
m_collectionwithin = value
End Set
End Property

Private m_payrollObject As New WageType
Public Property PayrollObject() As WageType
Get
Return m_payrollObject
End Get
Set(ByVal value As WageType)
m_payrollObject = value
End Set
End Property

Private m_payrollCollection As New WageTypeCollection
Public Property PayrollCollection() As WageTypeCollection
Get
Return m_payrollCollection
End Get
Set(ByVal value As WageTypeCollection)
m_payrollCollection = value
End Set
End Property

Public Function localSub() As Boolean

Return True
End Function

Public Function externalSub() As WageType

Return New WageType
End Function

End Class

Public Class ReferenceTestClassCollection
Inherits ObjectModel.Collection(Of ReferenceTestClass)

End Class

In the Payroll module:

Public Class WageTypye

End Class


Public Class TestingClass

Public Sub main()
Dim myRefClass As New TimeSheetModule.ReferenceTestClass

myRefClass.propWithin = "Hello"
myRefClass.PayrollObject = New WageType /HERE THE
ERROR LINE
End Sub

End Class


following the error visual studio throws:
Error 1 Reference required to assembly 'Payroll, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' containing type
'PayrollModule.WageType', but a suitable reference could not be found due to
possible circular references:
Assembly 'TimeSheet.dll' references project 'PayrollModule'
Project 'PayrollModule' references assembly 'TimeSheet.dll'.
D:\Development\PersonalPro\Modules\Payroll\Payroll\Classes\TestingClass.vb 9
9 PayrollModule


Can anyone help me to solve this problem? Have i screwed it up completely?
Thanks a lot in advance

ciccio
 
Ad

Advertisements

A

Armin Zingler

Ciccio said:
Hi all,

I have a question about circular references and how to avoid it.

You can not create a hen and an egg and assume the one must be there before
the other one and vice versa. So, you must put them into the same library.
Another way is building layers by writing more abstract code. That is a
broad topic and must be solved individually.
Asked in advance: Is there a reason why the classes are not in the same
library?

Please let me explain you quickly my problem with a short example.

I have two separate modules: payroll and timesheet. Each one of those
modules contains different classes. No to see through my problem i
try to simplify it

Why do you put classes in Modules? You'd better put them into a namespace.
Modules are specially handled classes and are not intended to be class
containers. I never use Modules - or only for extension methods (the one
marked with the <extension> attribute)


Armin
 
Ad

Advertisements

C

Ciccio

Good Morning and thanks a lot for the quick response.
Why do you put classes in Modules? You'd better put them into a namespace.
Modules are specially handled classes and are not intended to be class
containers. I never use Modules - or only for extension methods (the one
marked with the <extension> attribute)
Perhaps i chose the wrong word to describe it. What i called modules are in
fact two separated solutions with the corresponding reference (only the name
of the solutions are "wrong": payrollmodule, timesheetmodule)


Asked in advance: Is there a reason why the classes are not in the same
library?
Yes there is =)
The ReferenceTestClass I posted was just for testing reasons.
I have a WageType class and CalcWageTypeDetail class who inherits from
WageTypeDetail class. Those two classes are located in the payroll solution
(totatlly 3 classes). In a second solution called TimeSheet I use the
CalcWageType class for a field. Now from WageType i try to access the field
in TimeSheet who references the CalcWageTypeDetail -> circular reference
I see is not a praticable way...

Do you think I could solve my problems by sourcing out the
CalcWageTypeDetail in a third global solution (already existant) from where
all other classes could access?

Thanks a lot in advance
ciccio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top