How self-aware can an item in a complex nested class be?

G

Guest

I'm thinking of building a complex data model with a number of nested classes
but need to know something first. To simplify my question, let me present
you this sample data model

MainClass
Questions (collection)
Question (class)
Text (property)
Answers (collection)
Answer (class)
Text (property)


Here's what I'm looking to find out: Without specially preparing this data
model with all sorts of extra fields is there any way for the two properties
shown to know what index of the collection(s) they're in, should the property
value be changed?

For example, let's say that:
Questions(10).Text = "What's your favourite animal?"
and for this question:
Answers(3).Text = "Dog"

thus: Questions(10).Answers(3).Text = "Dog"

Now say that this property is changed externally by some process to "Cat".
What I would ideally like to do is have this change fire an event that will
inform all listeners (ie. forms) change "Dog" to "Cat".

But I can't envision how to do this if this property isn't aware of where it
sits in the hierarchy.

Any ideas?
 
S

SP

Robert W. said:
I'm thinking of building a complex data model with a number of nested
classes
but need to know something first. To simplify my question, let me present
you this sample data model

MainClass
Questions (collection)
Question (class)
Text (property)
Answers (collection)
Answer (class)
Text (property)


Here's what I'm looking to find out: Without specially preparing this data
model with all sorts of extra fields is there any way for the two
properties
shown to know what index of the collection(s) they're in, should the
property
value be changed?

For example, let's say that:
Questions(10).Text = "What's your favourite animal?"
and for this question:
Answers(3).Text = "Dog"

thus: Questions(10).Answers(3).Text = "Dog"

Now say that this property is changed externally by some process to "Cat".
What I would ideally like to do is have this change fire an event that
will
inform all listeners (ie. forms) change "Dog" to "Cat".

But I can't envision how to do this if this property isn't aware of where
it
sits in the hierarchy.

Any ideas?

You will need to pass the collection as one of the constructor parameters
for the item
public class Answer
{
private Answers answers;
public Answer(Answers answers)
{
this.answers = answers;
}

public int Index
{
get
{
return this.answers.IndexOf(this);
}
}
....

SP
 
N

Nick Malik [Microsoft]

Not so difficult. Answer is: don't make the form responsible for painting
the item... Make the item responsible for identifying itself and for
painting itself.

So if the other process changes Answer.Text from "Dog" to "Cat", then the
event contains a reference to Answer.MyIdentity (which could be a Guid or an
integer, or a random value or a known db id or anything that uniquely
identifies that particular answer from all other answers).

Then, in your form, you traverse the tree of Questions and Answers (I'd
suggest the Visitor pattern... see
http://www.dofactory.com/Patterns/PatternVisitor.aspx) and, when you find
the Answers in the tree, you pass in the Identity value and say "draw
yourself" if the identity matches. In other words, don't index directly...
search for it.

If you have less than 50 or so items on your form, this is not a noticably
less efficient idea.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
J

Jon Skeet [C# MVP]

Nick Malik said:
Not so difficult. Answer is: don't make the form responsible for painting
the item... Make the item responsible for identifying itself and for
painting itself.

I disagree with that, personally - it means you end up with
presentation logic (how an item should be drawn) mixed in with the
business logic (what a question is).

I'd be tempted to introduce an intermediate class, which knows how to
draw an answer and which answer it is associated with.
 
J

Joanna Carter \(TeamB\)

Robert W. said:
I'm thinking of building a complex data model with a number of nested classes
but need to know something first. To simplify my question, let me present
you this sample data model

MainClass
Questions (collection)
Question (class)
Text (property)
Answers (collection)
Answer (class)
Text (property)


Here's what I'm looking to find out: Without specially preparing this data
model with all sorts of extra fields is there any way for the two properties
shown to know what index of the collection(s) they're in, should the property
value be changed?

For example, let's say that:
Questions(10).Text = "What's your favourite animal?"
and for this question:
Answers(3).Text = "Dog"

thus: Questions(10).Answers(3).Text = "Dog"

Now say that this property is changed externally by some process to "Cat".
What I would ideally like to do is have this change fire an event that will
inform all listeners (ie. forms) change "Dog" to "Cat".

But I can't envision how to do this if this property isn't aware of where it
sits in the hierarchy.

Take a look at the Observer Pattern, and then look at how it is implemented
in .NET by using events and delegates.

Joanna
 

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