locked
foreach statement cannot operate on on variables of type 'object' because object does not contain a public definition for GetEnumerator RRS feed

  • Question

  • User-1410783915 posted

    I want to create a list of objects, store them in session, and then get them out of session and finally iterate through the list which I retrieve from my session variable.
    This appears to be working great, however when I try to iterate through my C# list, I get this compiler error message:

    foreach statement cannot operate on on variables of type 'object' because object does not contain a public definition for GetEnumerator

    The error occurs in my method Button1_Click method at the bottom of this post. Can someone tell me how I can fix this? My code is below.

    Public class Packet
    {
    public string id;
    public string name;

    public string ID
    {
    get { return id;}
    }

    public string Name
    {
    get { return name;}
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    Packet PackageOne = new Packet();
    PackageOne.id = "01";
    PackageOne.name ="Jeff";

    Packet PackageTwo = new Packet();
    PackageTwo.id = "02";
    PackageTwo.name ="Mary";

    List<Packet> list = new List<Packet>();
    list.Add(PackageOne);
    list.Add(Theirs);

    HttpContext.Current.Session["TheList"] = list;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    var Savedlist = Session["TheList"];
    foreach (var item in Savedlist) // Compiler : foreach statement cannot operate on on variables of type 'object' because object does not contain a
    { // public definition for GetEnumerator


    }
    }

    Friday, May 27, 2016 7:51 PM

Answers

  • User-286291038 posted

    Hi Gleeming,

    Please try casting the session value into a List<Packet>

    ie,

    var Savedlist = (List<Packet>)Session["TheList"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 27, 2016 9:26 PM
  • User36583972 posted

    Hi Gleeming,

    From your description, You can try the following three methods.

          protected void Button1_Click(object sender, EventArgs e)
            {
                //one
                var Savedlist = Session["TheList"];
                foreach (var item in (dynamic)Savedlist) // Compiler : foreach statement cannot operate on on variables of type 'object' because object does not contain a 
                {
                    var item2 = (Packet)item;
                    // public definition for GetEnumerator
                    Response.Write(item2.ID + "< 1 >" + item2.name + "  <br />");
                }
                //two
                var Savedlist2 = (List<Packet>)Session["TheList"];
                foreach (var item in Savedlist2) // Compiler : foreach statement cannot operate on on variables of type 'object' because object does not contain a 
                {
                    Response.Write(item.ID + "< 2 >" + item.name + "  <br />");
                    // public definition for GetEnumerator
                }
                //three
                var Savedlist3 = Session["TheList"];
                foreach (var item in (IEnumerable)Savedlist3)
                {
                    var item3 = (Packet)item;
                    // public definition for GetEnumerator
                    Response.Write(item3.ID + "< 3 >" + item3.name + "  <br />");
                }
            }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 30, 2016 5:31 AM

All replies

  • User-286291038 posted

    Hi Gleeming,

    Please try casting the session value into a List<Packet>

    ie,

    var Savedlist = (List<Packet>)Session["TheList"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 27, 2016 9:26 PM
  • User36583972 posted

    Hi Gleeming,

    From your description, You can try the following three methods.

          protected void Button1_Click(object sender, EventArgs e)
            {
                //one
                var Savedlist = Session["TheList"];
                foreach (var item in (dynamic)Savedlist) // Compiler : foreach statement cannot operate on on variables of type 'object' because object does not contain a 
                {
                    var item2 = (Packet)item;
                    // public definition for GetEnumerator
                    Response.Write(item2.ID + "< 1 >" + item2.name + "  <br />");
                }
                //two
                var Savedlist2 = (List<Packet>)Session["TheList"];
                foreach (var item in Savedlist2) // Compiler : foreach statement cannot operate on on variables of type 'object' because object does not contain a 
                {
                    Response.Write(item.ID + "< 2 >" + item.name + "  <br />");
                    // public definition for GetEnumerator
                }
                //three
                var Savedlist3 = Session["TheList"];
                foreach (var item in (IEnumerable)Savedlist3)
                {
                    var item3 = (Packet)item;
                    // public definition for GetEnumerator
                    Response.Write(item3.ID + "< 3 >" + item3.name + "  <br />");
                }
            }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 30, 2016 5:31 AM