Concept
Really it's about code design. E.g. whether to use inheritance, composition, or both. The answer will also depend on the peripherals of what you're trying to do (e.g. the code we don't see in your question).
Here's just one set of ideas about allowing your parts to reintegrate themselves to a parent context after their bytes are changed.
E.g.
Require the "parent" instance (I'll call it "context" in my example) to be passed to Header, Footer and Body when they are constructed; they can always contact the parent back at anytime to reassemble, access context members, or vice-versa. It's kind of like the Value property you mentioned but makes it immutable after construction.
Sample Implementation
We'll make BytesContext
read from the file and be responsible for splitting the bytes into other classes.
For example, Dim c as New BytesContext()
and c.ParseAllBytes()
method is called as follows:
Class BytesContext
Sub ParseAllBytes()
'READ ALL BYTES FROM FILE
'INSTANTIATE PARTS
' Each part takes a reference to this context.
Dim header As New HeaderBytes(Me)
header.Data = someOfTheParsedBytes
Dim body As New BodyBytes(Me, MaybeSomeBytesHere[])
Dim footer As New FooterBytes(Me)
..etc.
' Can always know the context.
Console.Write("Body context is " & body.Context.ToString())
End Sub
' A method that puts the pieces back together.
'
Sub PutBackTogether(part As BasePart)
If Typeof(part) Is HeaderBytes ...
...
ElseIf Typeof(part) Is FooterBytes...
... etc.
End Sub
End Class
HeaderBytes
(and the other kinds of parts) take a reference to the context as an instantiation argument - they all pass it to a base class for safe keeping (see next snippet):
Class HeaderBytes
Inherits BasePart
Sub New(ByVal context As BytesContext)
MyBase.New(context) 'Store in base class.
End Sub
''' REASSEMBLE SELF BACK INTO ORIGINAL
''' This instance can reference it's context and reintegrate its changes
Sub Save()
Context.PutBackTogether(Me)
End Sub
End Class
Class FooterBytes
Inherits BasePart
'...same...
Class BodyBytes
Inherits BasePart
'...same...
Note: the above part has the ability to reintegrate its changes into the original context.
This is the base part to share logic between parts, and keep a reference to the context for parts:
Class BasePart
'''Remember context.
Private _context As BytesContext
Public Sub New(ByVal context As BytesContext)
If _context Is Nothing Then Throw New ArgumentNullException("context")
_context = context
End Sub
''' Getter allows access to context.
ReadOnly Property Context() As BytesContext
Get
Return _context
End Get
End Property
End Class