2

I have an array and a loop, I want to find the current item's index so that I can decide whether to show that item in a listbox or not,

string[] papers = new string[7] { "Software Development", "Data Fundamentals", "Information and Communication", "User Support", "Web Fundamentals", "Network Fundamentals", "Computer Fundamentals" };

for (int i = 0; i < papers.Length; i++)
{
    int n=papers.Length;

    if (n==2)
    {
        continue;
    }
    else
    {
        listView1.Items.Add(papers[i]);
    } 
}

But I am unable to achieve it, could some one help me please... Or is there a better way to do this??

Thank you

1
  • 2
    Why don't you use if(n!=2) instead of checking for 2 and doing nothing? Commented Nov 11, 2012 at 0:38

1 Answer 1

6

The variable i is your current index.

for (int i = 0; i < papers.Length; i++)
{
   if (i==2)
      continue;
   else
      listView1.Items.Add(papers[i]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thnx for the reply. How will I be able to skip that Item from diplaying?
Why wouldn't you use if (i!=2) listView1.Items.Add(papers[i]);?
@raam030 Alex's code will definitely not create the item "Information and Communication". What are you trying to accomplish? Are you trying to loop through all of the array items and create a listview item for all of them except the item at index = 2?
@raam030 Good stuff. You may tick this as the answer if it is. You may also try Yatrix's suggestion to be more concise. And another comment would be to clear your list view items to ensure you are not duplicating stuff.
Alex, Yatrix thanks guys.. I am just trying create a listview which contains these items excluded in this looping.

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.