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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:12:27+00:00 2026-05-13T17:12:27+00:00

I have a problem with a foreach statement in my project. So far I

  • 0

I have a problem with a foreach statement in my project. So far I have the code:

    foreach(object i in listboxFiles.Items)
    {
        if (i == ".ftpquota")
        {
            listboxFiles.Items.Remove(i);
        }
        if (i == ".")
        {
            listboxFiles.Items.Remove(i);
        }
        if (i == "..")
        {
            listboxFiles.Items.Remove(i);
        }
    }

I have this in a 1 second timer. It gets the item name all right, but when it gets to the if statements it says that they do not match, but they do?

  • 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-13T17:12:27+00:00Added an answer on May 13, 2026 at 5:12 pm

    First thing, you are changing a collection while iterating over it. This cannot work, so your code is fundamentally broken.

    There are several ways to fix this; the simplest in your case would be to copy the items collection, iterating over the copy and changing (= removing from) the original:

    var items = new System.Collections.ArrayList(listboxFiles.Items);
    
    foreach (var item in items) {
        if (item.Equals("."))
            listboxFiles.Items.remove(item);
        …
    }
    

    Secondly, you are comparing an object to a string, hence the == operator does reference equality checking rather than testing for string equality. Either use Equals or do an appropriate cast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a continue statement in my C# Foreach loop. I
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
Hi I've got problem with foreach statement. I have my controller method: public ActionResult
I have this problem: $id is id for each user $img = 'http://www.somesite.com/pictures/'; $no_img
I have problem with my query on C, I’m using the oci8 driver. This
When I enter this foreach statement... foreach (var row in table.Rows) ...the tooltip for
I have a problem in the following code: $query = 'SELECT user.id as id,
I have this foreach loop, that I use for a search feature: $keywords=$_GET['keyword']; $exploded=explode('
So I have this problem, which I don't see how it's happening. Basically I
I have the following code : foreach( ... ) { $m = new Memcache;

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.