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 8339995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:02:44+00:00 2026-06-09T05:02:44+00:00

I have a static public List of type BoxPair which I’m trying to write

  • 0

I have a static public List of type BoxPair which I’m trying to write stuff to, but it changes every record every time I add a new entry into it. I’m hoping someone can see what’s going wrong here, as it’s driving me slightly insane. The full addition code is below:

public static List<BoxPair> CreateBoxPair (int iBoxCount)
{
    SetTopBox primary;
    SetTopBox backup;

    for (int i = 0; i < iBoxCount; i++)
    {
        primary = new SetTopBox();
        backup = new SetTopBox();

        primary.IBoxNumber = i;             
        primary.SDeviceName = "Box" + (i + 1).ToString("00");
        primary.Role = Box.ROLE_PRIMARY;

        backup.IBoxNumber = i;
        backup.SDeviceName = "Box" + (i + 1).ToString("00");
        backup.Role = Role.ROLE_BACKUP;

        lstBoxes.Add(new BoxPair(primary, Role));
        lstBoxes.Add(new BoxPair(backup, Role));

        foreach (BoxPair p in lstBoxes)
        {
            Declarations.BOXES.Add(p);
        }             
    }                        

    return lstBoxes;
}

I know it’s going to be something basic, but from what I can see, I’m always using a new primary/backup box, so nothing should conflict. Any ideas would be greatly appreciated.

Cheers.

P.S. Every post I make, SO removes the first word, so I have to type it twice. Any ideas as to why that happens?

EDIT: As all the approaches listed below have given the same result, I’m guessing that this is a problem that cannot be solved. Therefore, I’m returning to the drawing board to try and approach this differently. Thanks for all the help, anyways, chaps 🙂

  • 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-09T05:02:45+00:00Added an answer on June 9, 2026 at 5:02 am

    From the comments my best bet on code that works is as follows:

    public static List<BoxPair> CreateBoxPair (int iBoxCount)
    {
        for (int i = 0; i < iBoxCount; i++)
        {
            var primary = new SetTopBox();
            var backup = new SetTopBox();
    
            primary.IBoxNumber = i;             
            primary.SDeviceName = "Box" + (i + 1).ToString("00");
            primary.Role = Box.ROLE_PRIMARY;
    
            backup.IBoxNumber = i;
            backup.SDeviceName = "Box" + (i + 1).ToString("00");
            backup.Role = Role.ROLE_BACKUP;
    
            var primaryPair = new BoxPair(primary, Role);
            var backupPair = new BoxPair(backup, Role);
    
            lstBoxes.Add(primaryPair);
            lstBoxes.Add(backupPair);
    
            Declarations.BOXES.Add(primaryPair);
            Declarations.BOXES.Add(backupPair);
        }                        
    
        return lstBoxes;
    }
    

    I think there are two issues in the original code:

    • primary and backup being declared outside the loop.
    • The foreach loop inside the loop adding the entries to the other collection multiple times.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this bit of code, public static List<string> GetSentencesFromWords(List<string> words, string fileContents) {
I have a Generic List as below public static readonly List<Customer> Customers = new
i have the following code: public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
I have a static method which sets a variable: static String[] playersNames; public static
While executing this method: public static List<T> ToList<T>(DataTable dataTable) { Type type = typeof(T);
I have a static list: public static List<IMachines>mList =new List<IMachines>(); The list intakes two
I have the following Method public static List<T> MergeAndSort<T>(List<List<T>> listOfLists) where T : Foo
Let's say I have: public class Fruit { public static List<String> Suppliers { get;
I have one problem. i have dep. prop. of type List.. public const string
Suppose I have public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f) { return inputs.ConvertAll((x)

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.