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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:21:19+00:00 2026-05-14T18:21:19+00:00

Does anyone know about a good way to accomplish this task? Currently i’m doing

  • 0

Does anyone know about a good way to accomplish this task?

Currently i’m doing it more ore less this way, but i’m feeling someway unhappy with this code, unable to say what i could immediately improve.

So if anyone has a smarter way of doing this job i would be happy to know.

private bool Check(List<MyItem> list)
{
    bool result = true;
    //MyItem implements IComparable<MyItem>
    list.Sort();

    for (int pos = 0; pos < list.Count - 1; pos++)
    {
        bool previousCheckOk = true;
        if (pos != 0)
        {
            if (!CheckCollisionWithPrevious(pos))
            {
                MarkAsFailed(pos);
                result = false;
                previousCheckOk = false;
            }
            else
            {
                MarkAsGood(pos);
            }
        }

        if (previousCheckOk && pos != list.Count - 1)
        {
            if (!CheckCollisionWithFollowing(pos))
            {
                MarkAsFailed(pos);
                result = false;
            }
            else
            {
                MarkAsGood(pos);
            }
        }
    }
    return result;
}

private bool CheckCollisionWithPrevious(int pos)
{
    bool checkOk = false;
    var previousItem = _Item[pos - 1];

    // Doing some checks ...

    return checkOk;
}

private bool CheckCollisionWithFollowing(int pos)
{
    bool checkOk = false;
    var followingItem = _Item[pos + 1];

    // Doing some checks ...

    return checkOk;
}

Update

After reading the answer from Aaronaught and a little weekend to refill full mind power i came up with the following solution, that looks far better now (and nearly the same i got from Aaronaught):

public bool Check(DataGridView dataGridView)
{
    bool result = true;
    _Items.Sort();

    for (int pos = 1; pos < _Items.Count; pos++)
    {
        var previousItem = _Items[pos - 1];
        var currentItem = _Items[pos];

        if (previousItem.CollidesWith(currentItem))
        {
            dataGridView.Rows[pos].ErrorText = "Offset collides with item named " + previousItem.Label;
            result = false;
            sb.AppendLine("Line " + pos);
        }
    }

    dataGridView.Refresh();
    return result;
}
  • 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-14T18:21:20+00:00Added an answer on May 14, 2026 at 6:21 pm

    It’s certainly possible to reduce the repetition:

    private bool Check(List<MyItem> list)
    {
        list.Sort();
    
        for (int pos = 1; pos < list.Count; pos++)
        {
            if (!CheckCollisionWithPrevious(list, pos))
            {
                MarkAsFailed();
                return false;
            }
            MarkAsGood();
        }
        return true;
    }
    
    private bool CheckCollisionWithPrevious(List<MyItem> list, int pos)
    {
        bool checkOk = false;
        var previousItem = list[pos - 1];
    
        // Doing some checks ...
    
        return checkOk;
    }
    

    Assuming that CheckCollisionWithPrevious and CheckCollisionWithFollowing perform essentially the same comparisons, then this will perform the same function with a lot less code.

    I’ve also added the list as a parameter to the second function; it doesn’t make sense to be taking it as a parameter in the first function, but then referencing a hard-coded member in the function it calls. If you’re going to take a parameter, then pass that parameter down the chain.

    As far as performance is concerned, though, you’re re-sorting the list every time this happens; if it happens often enough, you might be better off using a sorted collection to begin with.

    Edit: And just for good measure, if the whole point of this code is just to check for some kind of duplicate key, then you would be way better off using a data structure that prevents this in the first place, such as a Dictionary<TKey, TValue>.

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

Sidebar

Related Questions

No related questions found

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.