M
Mark Broadbent
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.
//-------
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.