Suppose I have the following class:
Public Class lbMenu
Inherits Form
...
Public Sub AddItem(header As String)
Dim item As New lbMenuItem()
item.Owner = Me
item.Header = header
Controls.Add(item)
End Sub
...
End Class
and also the following class:
Public Class lbMenuItem
Inherits Control
Private _Owner As lbMenu
Public Property Owner As lbMenu
Get
Return _Owner
End Get
Set(value As lbMenu)
_Owner = value
End Set
End Property
Private _Header As String
Public Property Header As String
Get
Return _Header
End Get
Set(value As String)
_Header = value
End Set
End Property
...
End Class
As you can see, I access the Owner
property of lbMenuItem
from the class lbMenu
.
What I want is that nobody else can access the Owner
property, or maybe just to read the value of the property. I think there is no way to do that but maybe there is a solution to reach a similar situation.
Any suggestions?
Edit:
I already considered passing the owner as a parameter in the constructor, but I want that anyone can access the lbMenuItem
class. Anyone can create a new instance of the class, read and write many properties, etc. The only thing I don't want is that someone can write the Owner
property. Someone can create a new instance of the class without having to pass an ownerMenu
parameter to the New
method (he can't know what ownerMenu
is for). For the same reason, lbMenuItem
can't be a nested class of lbMenu
.
I'm trying to get the same result as with ContextMenuStrip
and ToolStripMenuItem
. ToolStripMenuItem
has an Owner
property (which in this case has read/write access) and an OwnerItem
which is read-only.
So, is there a way to make the Owner
property read-only but writable from the lbMenu
class?