Re: Class Hierarchy design problems...



David, Nick thanks for the input.
I agree (as you are both suggesting) that perhaps my problem is down to an
incorrect design, although I am not entirely sure it is as clean cut as
that.
The addition of a Remove method on the Items class would probably be a good
addition too i.e. Remove(Item item), however I do still think it makes
'sense' to have methods that fire on an object (in this scenario) which
operate in the context of the hierarchy.
For instance I would be saying that ... In that basket, choose an item (the
apple), and remove it.
You may both disagree entirely?

The question I would like to have answered though is this situation
possible?
The biggest problem is that the indexer is determining the subsequent
hierarchy due to its returned type (and therefore the methods would need to
exist on that type (which causes the problem of being able to reference the
items class).
The only work around I can think off is to create a new type called for
instance ItemEditor which I can instanciate and pass a reference to the
collection stored in the Items class . I could therefore return the
ItemEditor via the Items indexer (providing these methods). The problem
arises then that I would need to create an Item property in the Items editor
to return an object of type Item.
e.g.
class ItemEditor{
Item item;
Hashtable itemlist;
public ItemEditor(Hashtable itemcollection, Item item){...} //note
the constructor sets its localvars to these references so I can operate on
them
public Item
Item{ //item
property to return type Item
return item;
}
public void
Remove(){ //remove
method that can operate on collection within Items class
itemlist.Remove(item.Name);
}
}

//therefore class Items would need to be...
class Items{
Hashtable itemlist;
public Add(Item item){...}
public Item this[string item]{
get { Item i = itemlist[item];
return new ItemEditor(itemlist, item);
}
}
}

//Therefore the following would work as thus :-
//-------
Item i = Basket.Items["Orange"].Item;
Basket.Items["Apple"].Remove(); //method on class ItemEditor is fired

item i = new ("Pear");
Basket.Items.Add(item);
//------

Is there a way to do this any differently (i.e. to have the indexer
returning a certain type such as Items) but have addtional methods available
which are not present in Items? e.g.
Basket.Items["Apple"]; //returns type Item through indexer
Basket.Items["Apple"].Remove(); //item does not have remove
implemented (it is implemented elsewhere?)

It would be just good to know what is available to me.

Thanks guys,

Br,

Mark.
===================================





"Mark Broadbent" <nospam@xxxxxxxxxx> wrote in message
news:OphEyRNWFHA.2684@xxxxxxxxxxxxxxxxxxxxxxx
> Consider the following statements
> //-------
> Item i = Basket.Items["Orange"]; //indexer is used to return instance of
> class Item
>
> Basket.Items["Apple"].Remove(); //method on class item is fired
>
> item i = new ("Pear");
> Basket.Items.Add(item);
> //------
>
> Now my class design would be as follows
>
> class Basket{
> public Items Items{get;}
> }
> class Items{
> public Add(Item item){...}
> public Item this[string item]{get;}
> }
> class Item{
> public void Remove(){...} //problem is here
> }
>
> Now my problem is thus. If my Item class was self contained (in the sense
> that no other objects contain references to this item), I could implement
> the remove method to remove the item. Unfortunately the item must be
> deleted from a collection contained within Items. Problem is that since I
> need the indexer of Items to return a type of Item, the implementation of
> Remove must be put in Item *yet* there is no reference to the Items
> instance for me to be able to remove the item from the collection. How can
> I takle this?
>
> p.s. I should say that I do not want to implement a Remove method within
> items because I want to be able to use the indexer[].Remove design as
> above.
>
> Any ideas?
>
> Best Regards,
>
> Mark.
>
>
>


.



Relevant Pages

  • Sonar Design
    ... This is about the allocation of program resources, and distributed design ... This section is from chapter 2 of the reference book in Advanced ... PowerBuilder 4 Techniques, pp. 6 which describes high-level concepts used to ... when designing an architecture for a application. ...
    (comp.lang.misc)
  • Re: create design time library using vs.net
    ... design version and others are used for the run time version. ... > - copy the runtime library project file to a new design time project file ... You must add a reference to assembly 'System.Windows.Forms'. ... > The dependency 'Accessibility' could not be found. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Textbook text candidate
    ... Archaeologists do it all the time - by reference to known human ... You argue that it is possible to identify design in the abstract without ... But the only investigative programmme references HCMM. ... SETI assumes human-like techniques. ...
    (talk.origins)
  • Re: Call by reference in Java?
    ... > call by reference. ... > public void checkParameter(String strPreisProLiter, ... indicator that there is a flaw in your design. ... abstraction to fit the problem at hand. ...
    (comp.lang.java.help)
  • Re: Strongly Typed Collection Problem
    ... I sounds like you should really just have a Person class and use an indexer, ... so you could then reference: ... Person personBob = objPerson; ... On the next screen I retrieve the Person I ...
    (microsoft.public.dotnet.framework)