I have a class which get all the bytes from a file, then it splits up the bytes into three parts (a header, index and body) these 3 parts get passed along to 3 classes (called header, body and index) respectively. When the bytes in the three classes gets modified how can I pass these changes back up to the first class (the one that got the bytes from a file).
Must I have a property in each of the head, index and body classes called parentclass and then set the property when the classes are created?
This could get chaotic when the 3 classes are split up further.
A file is loaded and the bytes put into a property called data() then the bytes are split at certian places/offsets into three parts. One part gets put into a property called data() in a class called header the other two part are also put into properties called data but in another two classes called body and index.
The user, through the form ui, modifies the data in the 3 classes (header, index and body) I want this data to be passed back to the first class combined and then it can be saved as a file.
Or I just want to be able to referenc the data.
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.
alt text http://img194.yfrog.com/img194/278/bytesparts.png
Sample Implementation
We’ll make
BytesContextread from the file and be responsible for splitting the bytes into other classes.For example,
Dim c as New BytesContext()andc.ParseAllBytes()method is called as follows: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):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: