0

I have 2 arrays communes and arrListStr.

I'm using if else to compare array element communes vs array arrListStr, but I want communes array to have to go through all elements in array arrListStr if there is no duplicate to last position then exception.

I'm confused and don't know what to do? Thanks if someone help me !

Here is the code :

                /// communes list
               /// arrListStr list
               for (int a = 0; a < communes.Count; a++)
                {
                    for (int b = 0; b < arrListStr.Length; b++)
                    {
                        if (communes[a].Name == arrListStr[b])
                        {
                            count++;
                            rtbResult.AppendText("\n" + " Successfully - " + count);
                        }
                        else
                        {
                            count++;
                            rtbResult.AppendText("\n" + " Fail - " + count);
                        }
                    } 
                 }
1
  • I'm trying to understand rtbResult and count. The information you're logging doesn't seem useful because you don't know which pair it refers to. What is your goal with these tiems? Commented May 19, 2022 at 2:19

1 Answer 1

1

A simple solution would be to add a boolean to track if the item is found or not:

for (int a = 0; a < communes.Count; a++)
{

    bool isFound = false; // track if this item was found

    for (int b = 0; b < arrListStr.Length; b++)
    {
        if (communes[a].Name == arrListStr[b])
        {

            isFound = true; // mark as found

            count++;
            rtbResult.AppendText("\n" + " Successfully - " + count);
        }
        else
        {
            count++;
            rtbResult.AppendText("\n" + " Fail - " + count);
        }
    }


    // do something when not found
    if (!isFound)
    {
        throw new Exception("not found");
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.