views:

129

answers:

4

OK so here we go, one of my classes called Product is implemented like this:

namespace DomainModel.Entities
{
    public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }

    }
}

I want to add an IEnumerable to hold a list of values I can later iterate through in my view. How on earth do I do this and why is MSDN sample code so confusing (this doesn't help me at all and I have a feeling it's much simpler than this)?? (SEE BELOW)

using System;
using System.Collections;

public class Person
{
    public Person(string fName, string lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public string firstName;
    public string lastName;
}

public class People : IEnumerable
{
    private Person[] _people;
    public People(Person[] pArray)
    {
        _people = new Person[pArray.Length];

        for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new PeopleEnum(_people);
        }
}

public class PeopleEnum : IEnumerator
{
    public Person[] _people;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public PeopleEnum(Person[] list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Length);
    }

    public void Reset()
    {
        position = -1;
    }

    public object Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

class App
{
    static void Main()
    {
        Person[] peopleArray = new Person[3]
        {
            new Person("John", "Smith"),
            new Person("Jim", "Johnson"),
            new Person("Sue", "Rabon"),
        };

        People peopleList = new People(peopleArray);
        foreach (Person p in peopleList)
            Console.WriteLine(p.firstName + " " + p.lastName);

    }
}

/* This code produces output similar to the following:
 *
 * John Smith
 * Jim Johnson
 * Sue Rabon
 *
 */
+5  A: 

Whats wrong with a List<Person>?

svinto
+1 - 20 seconds!
John Rasch
A: 
List<Product> products = new List<Product>();
Rob
A: 

You dont look like you are going to need to extend IEnumerable

Try something like this:

static void Main()
{
    var peopleArray = new IEnumerable<Person>
    {
    new Person("John", "Smith"),
    new Person("Jim", "Johnson"),
    new Person("Sue", "Rabon"),
    };

    foreach (var p in peopleArray)
    { 
     Console.WriteLine(p.firstName + " " + p.lastName);
    }
}
Hurricanepkt
A: 

If all you want to do is add a collection of people (in one go) then the following is surely sufficient:

namespace DomainModel.Entities
{
    public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public IEnumerable<Person> People { get; set; }
    }
}

This is somewhat hacky in that you have to assign the people all in one go. You could make it an actual mutable collection like List instead, or provide an AddPerson() method to hide this (handling adding the same person more than once perhaps)

If this is based on a backing table though (I assume with some foreign Key relationship between People and Product you should consider letting the database access layer deal with this for you...

using the (very) simple suggestion above without any database backend/mapping would be as simple as:

public Product GetExampleProduct()
{
    return new Product() {
        ProductID = 1,
        Name = "SkeetOMatic",
        Description = "your very own Skeet in a box",
        Price = Double.PositiveInfinity,
        People = new[] { new Person("Joel"), new Person("Jeff") } 
    };
}

Simply using the proper foreign Keys in your database and using the Linq designer should allow you to note this association and let Linq handle writing all this boiler plate for you though. Then if you ask for the people related to this product you will get them pretty much automatically.

ShuggyCoUk