我的代码循环两次不确定我做错了什么。 [英] My code is looping twice not sure what I am doing wrong.

查看:53
本文介绍了我的代码循环两次不确定我做错了什么。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以上代码循环两次。有人可以纠正我的问题。谢谢。



我尝试了什么:



Stackoverflow,google,和教程指向。


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

What I have tried:

Stackoverflow, google, and tutorials point.

推荐答案

让我们遵循简单的逻辑。 1)在你的代码示例中,你只显示一个循环; 2)你认为它应该被执行一次,而你发现它发生了两次;让我们假设这是一个正确的观察结果。



我也很瘦我们可以假设你没有做出愚蠢的事情,例如明确地嵌套你在其他一些显示的循环循环并没有告诉我们。另请参阅:

奥卡姆剃刀 - 维基百科,免费百科全书 [ ^ ],

http://lurkmore.so/images/1/1b/Occam_razor.jpg [ ^ ]。 :-)



然后,我们可以得出一个合乎逻辑的结论:唯一合理的解释是你的问题不在这个代码之外,这是通过对某些人的调用来执行的包含代码示例的方法。请注意,仅显示代码片段并不总是足够的;当你必须显示整个方法(可能是不完整的)并且有时需要显示它的声明类型时,就是这种情况。有一些来自您期望的外部代码的调用,有些是您没想到的,错误的。调用方法时需要找出所有情况。



现在,剩下的问题是检测它。关于如何快速完成,我可以给你一个简单而通用的建议。显然,使用调试器,但如何使其快速?首先在有问题的循环的开头设置一个断点,也可以选择在方法的第一个语句上设置断点。然后在调试器下执行代码。当调试在第一行的断点处停止时,请执行以下操作:显示名为Call Stack的调试窗口



窗口是关键:您可以在第一次运行和以下所有情况下遍历堆栈并跟踪呼叫的来源。就这样。这真的很容易。



-SA
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


这篇关于我的代码循环两次不确定我做错了什么。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆