Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loop through and perform certain functions based on conditions?

Posted on 2006-07-06
6
Medium Priority
?
236 Views
Last Modified: 2010-04-23
Hello everyone,
I have the following code below. basically, I have a listcheckbox and a listbox. If Item 1 is selected, then I have it set to save to a file, if item 2 is selected, then it saves to a different file, and if both items are selected then its saved to another seperate file.

This works when I first run the program, if I select "item 1" and then hit my cmd button, it will save to that first file. If I change the checkbox to "item 2" and hit the cmd button, it still just save to the first file, instead of the file that is supposed to be saved when "item 2" is saved.

I am figuring I need some kind of loop in here, but I am not sure how to set it up where it loops through, and then saves to different files depending on what was selected.

Thanks!

        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")


        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")

        End If

        If wagesCheckBox.CheckedItems.Count() = 2 Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next



            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")

        End If
0
Comment
Question by:JasonWinn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Shakti109
ID: 17052512

There are several possibilities with this one.

First, when you do something like :

 If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20")

This is CasE SENsITIVE....As in :

If your listbox contains :  Labor costs-job Related:2 - 5205  Over $20
It will never evaluate to true as it is case sensitive and the two do not match.

Check your listbox contents very carefully (cut-n-paste if you are'nt sure, so they match).

Does the routine fail if you select both elements?
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 17052563
Hi Shakti,
Everything has been cut and pasted and works.

It does not fail, if I select one, then run it, and it does not fail if I select  both and run it.

If I load the program, and select "A" it does not fail
If I load the program, and select "B" it does not fail
If I load the program, and select "A" + "B" it does not fail

it fails when:
1) Load Program and Select "A"
2) hit Cmd button (still working at this point)
3) then i clear "A" (after running the report)
4) Select "B" and run the report (it runs the report as if "A" is selected, and not "B") <--- does not work at this point

the problem is pin-pointed at step 4. It run's the report on whatever was previously selected, regardless of what is currently selected when I run the report the 2nd time.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17053216
I'm not sure, but it looks like there may be some confusion between "selected" and "checked".  The first two "if" statements operate with a check on what is the .SelectedItem.  The last "if" statement does a test on .CheckedItems.  The description at the start of your post seems to refer interchangeably to "selecting" and "changing the checkbox".

Roger
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:JasonWinn
ID: 17053823
Hey guys,
I have figured out the problem, and cleared up a few things. I have changed it to a listbox so its not so confusing as well.

What is happening is that:

If Item 1 is selected then save itema.xml (works)
if item 2 is selected then save itemb.xml (works)
if item 1 + 2 is selected then save itemboth.xml (sorta works, it saves itemboth.xml and itema.xml) I need it to save just itemboth.xml and ignore the other subs.

The problem is that both item's are selected, and that 3rd If/Else works, but since "Item a" is still selected (since both items are selected), it also saves itema.xml. Is there a way to make it just save itemboth.xml and ignore the if/else for item a and item b if both are selected.

Thanks
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 17053828
       Dim xDoc As XmlDocument
        xDoc = New XmlDocument

        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")


        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")

        End If

        If wagesCheckBox.SelectedItems.Count = 2 Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next



            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")

        End If
0
 
LVL 8

Accepted Solution

by:
Shakti109 earned 2000 total points
ID: 17054581

The most simple way is to re-order your if-then selections, and add in an exit sub after each. LIKE :

        If wagesCheckBox.SelectedItems.Count = 2 Then

           xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
            Dim fullNameNode2 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode2 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            ReportEntityFilterNode2.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName4 As XmlNode = xDoc.CreateElement("FullName")
                newName4.InnerText = item.ToString()
                ReportEntityFilterNode2.AppendChild(newName4)
            Next

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailBoth.xml")
        exit sub
        End If

       If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:2 - 5205  Over $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
            Dim fullNameNode1 As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode1 As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode1.RemoveAll()
            ReportClassFilterNode1.RemoveAll()


            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName2 As XmlNode = xDoc.CreateElement("FullName")
                newName2.InnerText = item.ToString()
                ReportEntityFilterNode1.AppendChild(newName2)
            Next


            Dim newName3 As XmlNode = xDoc.CreateElement("FullName")
            newName3.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode1.AppendChild(newName3)

            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailOver20.xml")
        exit sub
        End If


        If wagesCheckBox.SelectedItem.Equals("Labor Costs-Job Related:1 - 5201 Under $20") Then


            xDoc.Load("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")
            Dim fullNameNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//FullName")
            Dim ReportEntityFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportEntityFilter")
            Dim ReportClassFilterNode As XmlNode = xDoc.SelectSingleNode("//ReportClassFilter")
            ReportEntityFilterNode.RemoveAll()
            ReportClassFilterNode.RemoveAll()

            For Each item As String In Me.customerListBox.SelectedItems
                Dim newName As XmlNode = xDoc.CreateElement("FullName")
                newName.InnerText = item.ToString()
                ReportEntityFilterNode.AppendChild(newName)
            Next

            Dim newName1 As XmlNode = xDoc.CreateElement("FullName")
            newName1.InnerText = wagesCheckBox.SelectedItem
            ReportClassFilterNode.AppendChild(newName1)


            xDoc.Save("C:\Documents and Settings\jwinn\My Documents\XML\payrollDetailUnder20.xml")

        exit sub
        End If

There are other ways to do this, rolling everything into one if-then-else condition set for example (in which you will still have to re-order the conditions such that the "combined" condition is first), but simply adding the "combined" condition as the first check, and exiting the sub they are part of is the most direct.

If you have other work to do AFTER the checks in the same subroutine, then you can :
1) move the if-then checks to a function, and exit the function after each call.
2) move the if-then checks to another subroutine and leave it the way it is.
3) change the "exit sub" to a goto marker within the same subroutine AFTER all of the checks (as in goto ChecksCompleted, etc).
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question