代码检查错误 [英] Code checking for mistakes

查看:70
本文介绍了代码检查错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用While循环来搜索按钮单击时的单词。但contextMenu在不停止或显示结果的情况下继续快速上下跳动或加载。以下是代码:



 私有  Sub  CheckForReplacementText()

如果 nextCheckIndex = replacementments.Count 然后

MessageBox.Show( 检查完成。
nextCheckIndex = 0

结束 如果





我尝试过:



使用while循环

解决方案

为什么要尝试以递归方式执行此操作,当它是循环时问题?递归仅在将值作为参数传递给方法时才有用,因此不同的迭代具有独立的值。

您所做的就是调用相同的方法,并在返回时退出 - 所以循环在这里是一个更明智的解决方案。

如果在任何时候nextCheckIndex大于replacements.Count和foundIndex是零或更多...你的代码将永远递归,你将获得一个堆栈溢出 - 正如你所发现的那样。

试一试:使用调试器,你会看到我的意思。



不要使用递归除非它是真正需要它的任务,或者表现出递归结构的数据,例如处理文件夹的所有子目录中的所有文件。这不是 - 它只是一串数据,你正在寻找要改变的位...


调试你的代码逐行逐行,这将显示出什么问题是。例如,replacementments.Count是什么?如果它是0那么你的代码将永远循环,但只有你可以知道。如果这是问题,请尝试



 如果 nextCheckIndex =替换.Count  replacementments.Count =  0  然后 

MessageBox.Show( 检查完成。
nextCheckIndex = 0

结束 如果


I have tried to use a While Loop to search for words on button click. But the contextMenu keeps on jumping up and down or loading very quickly without stopping or showing the results. Here is the code:

Private Sub CheckForReplacementText()

    If nextCheckIndex = replacements.Count Then

        MessageBox.Show("Check complete.")
        nextCheckIndex = 0

    End If



What I have tried:

To use a while loop

解决方案

Why are you trying to do this recursively, when it's a loop problem? Recursion is only useful when you pass a value to the method as a parameter, so that different iterations have an independent value.
And all you do is call the same method, and exit when it returns - so a loop is a much more sensible solution here.
And if at any time nextCheckIndex is greater than replacements.Count and foundIndex is zero or more ... your code will recurse forever and you will get a stack overflow - as you have found.
Try it: use the debugger and you will see what I mean.

Don't use recursion unless it's a task which genuinely needs it, or data which exhibits a recursive structure such as processing all the files in all the subdirectories of a folder. This doesn't - it's just a string of data and you are looking for bits to change...


Debug your code to step through it line by line and that will show what the problem is. What is "replacements.Count" for example? If it is 0 then your code will loop forever, but only you can know that. If that is the issue try

If nextCheckIndex = replacements.Count Or replacements.Count = 0 Then

    MessageBox.Show("Check complete.")
    nextCheckIndex = 0

End If


这篇关于代码检查错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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