Results 1 to 3 of 3

Thread: My list of objects isn't enumerable

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Resolved My list of objects isn't enumerable

    OK, I have an invoice object.
    Within that object I have an InvoiceHead object and list of InvoiceDetail Objects.

    Below is my code to create these classes in this order.
    The invoice - which has a head, detail List (invoice lines) and then code for a single detail in it (so in the UI can create a single line)
    The InvoiceHead object - contains data that is the same for the whole invoice - like invoice number
    The InvoiceDetailList object (lines on the invoices) - which is a list of details
    The InvoiceDetail object (a single line on the invoice)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyObjects
    {
    [Serializable]
    public class MyApInvoice
    {
    public Int32 IsSuccessful { get; set; }
    public string Message { get; set; }
    public Int32 DetailLength { get; set; }
    public MyAPInvoiceHead MyApInvHeader = new MyAPInvoiceHead();
    public MyAPInvoiceDetail MyApInvDetail = new MyAPInvoiceDetail();
    public MyAPInvoiceDetailList MyApInvDetailList = new MyAPInvoiceDetailList();

    public MyApInvoice()
    {

    }
    }

    [Serializable]
    public class MyAPInvoiceHead
    {
    public string Company { get; set; }
    public string InvoiceNum { get; set; }
    public DateTime InvoiceDate { get; set; }
    }

    [Serializable]
    public class MyAPInvoiceDetailList
    {
    public int Length { get; set; }
    public List<MyAPInvoiceDetail> invoiceLine = new List<MyAPInvoiceDetail>();
    }
    [Serializable]
    public class MyAPInvoiceDetail
    {
    public string Company { get; set; }
    public string InvNum { get; set; }
    public Int32 InvLine { get; set; }

    public MyAPInvoiceDetail()
    {

    }
    }

    }
    Now, I create the invoice, populate the invoice details with no problem. I actually use a for loop to do populate the list.

    Now I want to loop through the list later in my code with a foreach statment. But I get an error saying the list isn't enumerable.

    Here's the code I use to try to enumerate the detail list.

    // create the detail list object called apInvDtlList and populate the lines in the list with a for loop

    ...

    // now loop/enumerate through this list with a foreach statement
    foreach (MyAPInvoiceDetail dtl in apInvDtlList)
    {

    }
    But that is where I get the error. My list isn't enumerable. I'm sort of unclear on this because I thought that all lists were enumerable. But I've ran into this before so it would seem I'm wrong. Not all list are enumerable. But I'm pretty sure I can make it enumerable. But how?

    What am I missing? What can I do to make my list enumerable?
    Last edited by sooner; Feb 4th, 2013 at 09:11 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: My list of objects isn't enumerable

    Firstly, please don't use [quote] tags for code. [quote] tags are for quotes. Guess what you use for code. That's right: [code] tags.

    As for the issue, your class MyAPInvoiceDetailList is not enumerable because it's not a list. It has a field 'invoiceLine' that is a list, but it is not a list itself.

    That class is incorrectly defined. If you want your class to be enumerable then it has to either implement the IEnumerable interface or inherit a class that does. To that end, Microsoft has provided the System.Collections.ObjectModel.Collection<T>. Your class should simply look like this:
    Code:
    [Serializable]
    public class MyAPInvoiceDetailList : System.Collections.ObjectModel.Collection<MyAPInvoiceDetail>
    {
    }
    You don't need any code at all if all you want is a standard list because all that is inherited from Collection<T>. You only need to add code if you need custom behaviour. The Collection<T> class provides some protected methods that you can override to perform processing when items are added, removed, etc.

    By the way, you should pretty much never have public fields. If you were going to use your original MyAPInvoiceDetailList class then it should be like this:
    Code:
    [Serializable]
    public class MyAPInvoiceDetailList
    {
        public List<MyAPInvoiceDetail> _invoiceLine = new List<MyAPInvoiceDetail>();
    
        public int Length { get; set; }
        public List<MyAPInvoiceDetail> InvoiceLine { get { return _invoiceLine; } }
    }
    That's how classes that contain lists are implemented through out the .NET Framework, e.g. DataSet.Tables, DataTable.Rows, DataTable.Columns, Control.Controls, ComboBox.Items, etc, etc, etc.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Resolved Re: My list of objects isn't enumerable

    jmcilhinney, thanks for you help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width