473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
4 1148
SP

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
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
Nov 17 '05 #2
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.
--
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #3
Nick Malik [Microsoft] <ni*******@hotmail.nospam.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
"Robert W." <Ro*****@discussions.microsoft.com> a écrit dans le message de
news: 1F**********************************@microsoft.com...
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

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
1
by: Vijay | last post by:
Hi Can anybody explain what self::* means in Xpath. I understand that "self" means the context node itself and * means any node type. So I thought self::* meant all the child nodes of context,...
7
by: Philip | last post by:
Hi all, Been having a really tricky problem. I'm trying to do some object oriented programming with javascript and it was all working fine until I had to support Netscape 4. The problem is...
18
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object):...
4
by: Wow | last post by:
when calling a object in an html, should I use self.objectname this.objectname or document.objectname? for example, i have a form called theform and a link called thelink i can call...
4
by: Phil Powell | last post by:
I thought this would work but it seems to not work neither in Netscape nor in IE: <script type="text/javascript"> <!-- // OBTAINED FROM http://www.javascripter.net/faq/settinga.htm //...
16
by: paullanier | last post by:
It seems that lots of people don't like having to prefix self. in front of instance variables when writing methods in Python. Of course, whenever someone suggests doing away with 'self' many...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
3
by: Aaron Gray | last post by:
Regarding 'self' :- if (self) document.write( "self<br>"); if (self === window) document.write( "self === window<br>"); else if (self == window) document.write( "self == window<br>");
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.