Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:02:24+00:00 2026-06-04T02:02:24+00:00

The issue I’m noticing is this line of code: tempList.Add(orderables); In this full code:

  • 0

The issue I’m noticing is this line of code:

tempList.Add(orderables);

In this full code:

AssociatedComboItems ai = new AssociatedComboItems();
List<Orderables> tempList = new List<Orderables>();
Orderables orderables = new Orderables();

foreach (var t in comboBox1.Items)
{
    ai.ComboBoxItem = t.ToString();

    for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
    {
        orderables.Display = fpSpread1.ActiveSheet.Cells[i, 1].Text;
        orderables.ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value;
        orderables.DisplayOrder = i;
        tempList.Add(orderables);
    }

    ai.AssociatedItems = tempList;
    tempList.Clear();
    if(AssociatedItems == null)
    AssociatedItems = new List<AssociatedComboItems>();
    AssociatedItems.Add(ai);
}

When I put my breakpoint on the line mentioned above (tempList.Add(orderables); ), the first time it adds the item correctly to the templist and it will have one item in it. The second time it will add the correct item to the list but if I hover over tempList and want to see its contents, although it has two items, both of them are the same – they are both now the second item that was added to the list. It has overwritten the first one.

I can’t figure out what is going wrong with this and why it is happening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:02:25+00:00Added an answer on June 4, 2026 at 2:02 am

    You need to instantiate the Orderables within the for loop; otherwise, you keep reusing the same instance in all iterations (and overwriting its properties each time).

    AssociatedComboItems ai = new AssociatedComboItems();
    List<Orderables> tempList = new List<Orderables>();
    
    foreach (var t in comboBox1.Items)
    {
        ai.ComboBoxItem = t.ToString();
    
        for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
        {
            Orderables orderables = new Orderables();  // ← Instantiate here
            orderables.Display = fpSpread1.ActiveSheet.Cells[i, 1].Text;
            orderables.ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value;
            orderables.DisplayOrder = i;
            tempList.Add(orderables);
        }
    
        ai.AssociatedItems = tempList;
        tempList.Clear();
        if(AssociatedItems == null)
        AssociatedItems = new List<AssociatedComboItems>();
        AssociatedItems.Add(ai);
    }
    

    Unrelated to the question: You might find object initializer syntax to be cleaner:

    Orderables orderables = new Orderables
    {
        Display = fpSpread1.ActiveSheet.Cells[i, 1].Text,
        ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value,
        DisplayOrder = i,
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
issue is this: in (pl)python code, we've calculated an integer = 26663. Can easily
issue is with the ShowPath(); method as it keeps overloading this code is supposed
Issue In the below code , say if i want to change the color
This issue has me baffled, it's affecting a single user (to my knowledge) and
ISSUE: code that worked to close and destroy a cfwindow after form submit -
Issue I have an aspx page with jQuery code to send an ajax request
Strange issue. This is index.php: session_start(); print_r($_COOKIE); print_r($_SESSION); This is logout.php: session_destroy(); $_COOKIE['key'] =
This issue came up when I got different records counts for what I thought
Issue 2. I am now getting an error in where the code is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.