Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 859
  • Last Modified:

Question regarding Foreach loop with Listview Items

Here is my problem. I am sure it is probably something simple.

Say my listview has 6 items in it.

The problem I am running into is the whole last item #5 or 6 is not getting written out to the XML if I delete either #5 or 6 from the listview.  If I delete item 1-4 it works fine. When I delete 5 or 6 then the whole last block does not get written out.

The way my XML file should work is item 1+2 contain the same info minus 3 fields so they are linked as one. They are all paired up 3+4 and 5+6 What should happen is if I delete #5 the XML should show 1+2, 3+4, and 5 alone.

Can someone tell me what I am doing wrong in this loop. I have been working on it for a while.

         string[] PreviousRowArray = null;

         foreach (ListViewItem row in listViewFount.Items)
         {
            string CurrentRow = "";

            for (int i=0 ; i<row.SubItems.Count ;i++)
            {
               CurrentRow = CurrentRow + row.SubItems[i].Text + " ";
            }

            string[] CurrentRowArray = CurrentRow.Split(' ');
               
            if (PreviousRowArray != null)
            {
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
               {
                  //Here I write out the CurrentRowArray information combined with
                  //the PreviousRowArray. Removed for ease of reading
                 
                  PreviousRowArray = null;
                  CurrentRowArray  = null;
               }
               else
               {
        //Here I write only the pervious line out to the
                  //XML file. Removed for ease of reading

                  PreviousRowArray  = CurrentRowArray;
               }
            }
            else//Move currentrow to previous

               PreviousRowArray = CurrentRow.Split(' ');
         }

         //this is the end of the document
         textwriter.WriteEndDocument();              
         textwriter.Flush();
         textwriter.Close();

      }


0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 2
1 Solution
 
wlfsCommented:
Hi, I only had a quick look up to now. What catched my eyes is the if condition
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
The two sides of the && are identical. I guess it should be
               if ((CurrentRowArray[0].ToString() == PreviousRowArray[0].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
Does this solve your problem?
0
 
NewMom2BrandonAuthor Commented:
hmm that was a good problem (duh kicking myself) but it still doesn't solve my actual issue.
0
 
junglerover77Commented:
I think that's obvious you item #5 will Not be written out if #6 is removed.

Just imagine:
when the loop goes to #4, because #3 and #4 matches, the following code will be executed:
                  //Here I write out the CurrentRowArray information combined with
                  //the PreviousRowArray. Removed for ease of reading
                 
                  PreviousRowArray = null;
                  CurrentRowArray  = null;

so, when the loop goes to #5, the value of "PreviousRowArray" is null. so it goes to:

            else//Move currentrow to previous

               PreviousRowArray = CurrentRow.Split(' ');

then the loop is ended because #5 is already the last element. So, nothing will happen.

Just add the following codes before your last 3 statements (after the loop), just like this:

         if (PreviousRowArray != null) {
         //Here I write only the pervious line out to the
                  //XML file. Removed for ease of reading
         }

         //this is the end of the document
         textwriter.WriteEndDocument();              
         textwriter.Flush();
         textwriter.Close();

Jungle
0
 
NewMom2BrandonAuthor Commented:
thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now