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

  • Home
  • SEARCH
  • 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 6241935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:51:28+00:00 2026-05-24T11:51:28+00:00

I am currently working on a C# WPF project. I have a list which

  • 0

I am currently working on a C# WPF project. I have a list which uses a class to store multiple values. The class is called DBTables and has the following inside:

class DBTables
{
 public string selDatabase { get; set; }
 public string selTable { get; set; }
}

I am creating a new instance of the list with the following code

List<DBTables> tableArr = new List<DBTables>();

I am adding new items to the List without any problems but the problem I am having is when it comes to removing an item from the list.

A an item is added to the list when a checkbox is selected the item is added and when the checkbox is unchecked the item needs to be removed. Each time the checkbox is checked two values are added using the following code:

private void addBackupArray(string table)
{
    backupArr.Add(new DBTables
    {
        selDatabase = selectedDatabase,
        selTable = table
    });
}

When the check box is unchecked the values at the position need to be removed and I have sort of got it working but after it has removed the item it then displays the error ‘InvalidOperationException, collection was modified; enumeration may not execute’.

Below is the code that I am currently using to remove the item from the list.

private void removeBackupArray(string table)
{
    int i = 0;
    foreach (DBTables tables in backupArr)
    {
        if (selectedDatabase == tables.selDatabase && table == tables.selTable)
        {
            backupArr.RemoveAt(i);
            i = 0;
        }
        i++;
    }
}

The code above iterates through the values in the list and based on an if statement of whether the two variables match the value found in the list it removes it at the current position of the counter i.

How can I get round this issue so I can remove the item without getting the error.

Thanks for any help you can provide.

  • 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-05-24T11:51:30+00:00Added an answer on May 24, 2026 at 11:51 am

    Change the foreach to normal for loop will fix the issue:

    for (int tablesIndex = 0; tablesIndex < backupArr.Count; tablesIndex++)
    {
        var tables = backupArr[tablesIndex];
    
        if (selectedDatabase == tables.selDatabase && table == tables.selTable)
        {
            backupArr.RemoveAt(tablesIndex);
            tablesIndex--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a project of mine which is a MSPaint-like WPF
I'm currently working on a project where we have a lot of dependencies. I
Hy guys! I am currently working on a little WPF project using MVVM via
I'm currently working on a WPF/C# application which is connected to an external camera.
I'm currently working on a WPF UI and I have a combobox on my
I'm working on my first true WPF MVVM pattern application. Currently I have a
I am currently working on a project and my goal is to locate text
I'm currently working on creating a new C# project that needs to interact with
I am currently working on a project with specific requirements. A brief overview of
The system I am currently working on requires some role-based security, which is well

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.