Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
GlobalVariable.containsListBox2Item.Clear()
        For i As Integer = 0 To ListBox2.Items.Count - 1
            If GlobalVariable.lbl1(0).ToString = "X,Y Coordinate" Then
                GlobalVariable.containsListBox2Item.Add("X,Y Coordinate".ToString)
            End If
            If GlobalVariable.lbl1(1).ToString = "Latitude, Longitude" Then
                GlobalVariable.containsListBox2Item.add("Latitude, Longitude".ToString)
                'globalvariable.containslistbox2item(1) = "x,y coordinate"
            End If
        Next

Code above is looping twice. Can someone please correct my issue. Thanks.

What I have tried:

Stackoverflow, google, and tutorials point.
Posted
Updated 6-May-16 9:16am
Comments
Sergey Alexandrovich Kryukov 6-May-16 15:04pm    
Nothing is wrong; you just loop it twice.
—SA
JT1992 6-May-16 15:05pm    
how do i loop this once i am not sure?
Sergey Alexandrovich Kryukov 6-May-16 15:16pm    
I answered in detail in Solution 1.
—SA
JT1992 6-May-16 15:28pm    
please upvote the question and see my comment below. you rock!

1 solution

Let's follow the simple logic. 1) in your code sample, you show exactly one loop; 2) you maintain that it should be executed once while you observe that it happens twice; let's assume this is a correct observation.

I also thin that we can assume that you are not making something silly, such as explicitly nesting the loop you show in some other loop and failing to tell us about it. See also:
Occam's razor — Wikipedia, the free encyclopedia[^],
http://lurkmore.so/images/1/1b/Occam_razor.jpg[^]. :-)

Then, we can make a logical conclusion: the only reasonable explanation is that your problem lie outside of this code, which is executed via some call to some method containing your code sample. Note that showing just the code fragment is not always enough; your case is the case when you had to show the whole method (maybe incomplete) and sometimes its declaring type. There is some call from outside code you expected and some you did not expect, mistakenly. You need to find out all cases when the method is called.

Now, the remaining problem is to detect it. I can give you a simple and universal advice on how to do it quickly. Obviously, use the debugger, but how to make it quick? First put a break point on the beginning of your loop in question, and also, optionally, on the first statement of your method. Then execute the code under the debugger. When debugging stops at the break point for the first line, do the following: show the debug window called "Call Stack".

This window is the key: you can walk the stack and track where the call comes from, both at first run and all following cases. That's all. This is really easy.

—SA
 
Share this answer
 
v2
Comments
JT1992 6-May-16 15:27pm    
lmao. you rock! that is exactly what i was doing nesting the loop in another loop.
JT1992 6-May-16 15:28pm    
upvote this question this needs to go viral with your reply lol
Sergey Alexandrovich Kryukov 6-May-16 15:34pm    
First, you are very welcome; and thank you for your nice apply.
I suggest you also accept the answer formally. This is really all you need to know on the topic.
(I hope you can do it despite the fact that, as you admitted, I actually made a wrong assumption. :-)
—SA
ridoy 6-May-16 15:40pm    
Oh, great! my 5.
Sergey Alexandrovich Kryukov 6-May-16 15:44pm    
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900