views:

323

answers:

5

Hello, for a project i'm currently working on, im trying to pass a set of objects all somehow linked to one object.

I'm currently blacked out and can't seem to find a proper solution.

The situation is like this. I have a product object, on which a few insurance packages apply. I need to pass this information to the view, but I have to be able to retrieve the proper packages for the proper products. so it looks like this...

Product 1 has package 1, 2, 3 Product 2 has package 2,3,5,6 Product 3 has package 2,4,6,7

The problem is that there can be a different number of products and a different number of packages

Any ideas? The answer is probably simple, but I'm a little bit too tired to find it out...

A: 

From the sound of it you need to wrap the objects up in some container object. The container object constructor would take all the objects you want to group together, then you can pass the container object.

If your passing things up to a view via an event, perhaps you could use a custom class that inherits from the EventArgs class.

Simon P Stevens
A: 

I think we need more information... Are your product and package objects linked in any way? If the product object has a list of packages that apply then you simply pass the product to the view and move on. If not then how are you keeping track of the linkage between your two object types?

NJChim
+5  A: 

A dictionary of lists?

Dictionary<Product, List<Package>> products = new Dictionary<Product, List<Package>>();
products.Add(product1, new List<Package>());
products.Add(product2, new List<Package>());
products.Add(product3, new List<Package>());
products[product1].Add(package1);
products[product1].Add(package2);
products[product2].Add(package2);
products[product2].Add(package3);
products[product2].Add(package5);
products[product2].Add(package6);
products[product3].Add(package2);
products[product3].Add(package4);
products[product3].Add(package6);
products[product3].Add(package7);

I do this often enough I wrote my own container IndexedLists<K,V> which is really a Dictionary<K, List<V>> that automatically handles creation of the lists.

Colin Burnett
Beat me to it. :) +1.Alternatively place List<Package> within your Product class. The use ListView to display.
ChrisBD
A: 

How about this:

  • Create a container class, as Simon suggested
  • Inside the class, have these members:
    • List Products { get; set; }
    • List Packages { get; set; }
    • List GetPackages(Product product);

The GetPackages method can query the internal dictionary to determine which packages to return.

public class ProductsPackages
{
    private Dictionary<int, int> _map;

    public ProductsPackages(List<Product> products, List<Package> packages,
                            Dictionary<int, int> map)
    {
        _map = map;
    }

    public List<Product> Products { get; private set; }
    public List<Package> Packages { get; private set; }

    public List<Package> GetPackages(Product product)
    {
        return (from p in Packages
                join kvp in _map on p.ID == kvp.Value
                where kvp.Key == product.ID
                select p).ToList();
    }
}
Nathan Ridley
Why would you want to map list indexes to list indexes? Why not map them explicitly? (Also, GetPackages has no return type.)
Colin Burnett
He has an obvious many-to-many relationship between products and packages; that's all that was intended by the dictionary. He should of course adapt to suit his needs, but the solution still serves to illustrate a solution.
Nathan Ridley
A: 

Perhaps I misunderstand the question, but if it's simply a matter of passing these data to the view, the most straightforward might be to create a method which takes a product as one parameter and a collection of packages as another parameter, something like:

public void SetProduct(Product product, IList<Package> packages)
{
   ...
}
Tor Haugen