While building on a class library for an VB.NET object model, I get
something similar to the above error message, but not with C#.
The steps to recreate the problem are as follows:
1. Build a RootLevel.dll containing only this code:
using System;
namespace RootLevel {
public class Root {
}
}
2. Build a SecondLevel.dll containing a reference to RootLevel.dll and only
this code:
using System;
namespace SecondLevel {
public class Child {
private RootLevel.Root _root;
public RootLevel.Root Root {
get {
return _root;
}
set {
_root = value;
}
}
}
}
3. Edit the Root class, add a reference to SecondLevel.dll and Re-build the
RootLevel.dll. The edited code looks like this:
using System;
namespace RootLevel {
public class Root {
private SecondLevel.Child _child1; //there will be other children
public SecondLevel.Child Child1 {
get {
if (_child1 == null) {
_child1 = new SecondLevel.Child();
_child1.Root = this;
}
return _child1;
}
}
}
}
And it builds without an error. Unfortunately, using VB.NET, it doesn't.
1.
Public Class Root
End Class
2.
Public Class Child
Private _root As RootLevelVB.Root
Public Property Root() As RootLevelVB.Root
Get
Return _root
End Get
Set(ByVal value As RootLevelVB.Root)
_root = value
End Set
End Property
End Class
3.
Public Class Root
Private _child1 As SecondLevelVB.Child 'there will be other children
Public ReadOnly Property Child1() As SecondLevelVB.Child
Get
If _child1 Is Nothing Then
_child1 = New SecondLevelVB.Child
_child1.Root = Me
End If
Return _child1
End Get
End Property
End Class
I believe my conversion from VB.NET to C# is 100% equivalent.
The reference to 'Me' in VB.NET step 3 is marked with the following error by
the Visual Studio.NET IDE:
"Reference required to assembly 'RootLevel' containing the type
'RootLevel.Root'. Add one to your project."
Obviously, this a simplified example, where children are plugged into the
root as needed. The children can be pretty complex and considerably
different from each other. Children need to reference the root, and children
often need to reference each other.
How can I fix the VB.NET situation. Is this a VB.NET compiler bug or a
default C# setting that differs? How do I work around this circular
compilation reference situation or whatever is going on here?
Thanks,
Kelly