在for循环中声明的变量。我如何使这个编译时错误? [英] Variable declared inside a for loop. How do I make this to a compile time error?
问题描述
昏暗的数字作为新列表(整数)从{1,2,3,4,5}
For每个数字As Integer数字
Dim isEven作为布尔
如果数字Mod 2 = 0那么
isEven = True
End If
如果isEven Then
Console.WriteLine(number.ToString()&is Even)
else
Console.WriteLine(number.ToString()& amp; ;is Odd)
End If
Next
产生以下结果:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'是Even
5是Even
问题在于 isEven
是已声明但未分配的。
在这个特定情况下,将 dim isEven作为布尔值= false
是正确的,但是我还没有做到这一点。
在VB.NET中,在for循环中声明的变量为下一次操作保留了它的值。这是由设计: http://social.msdn.microsoft但是这对于程序员来说也是一个危险的陷阱。 不过,直到现在,我还没有意识到这个问题/行为。到现在。 我不要以为我们的开发团队中的任何人都曾经有过这样的目的。如果我明确地想要在一个for循环中迭代一个变量,我声明它在范围之外。 所以最好的办法是产生一个警告,甚至是一个警告在这个特定情况下的错误。 有没有办法让这个编译时错误或者是一个如何用FxCop来检查这个问题? 我不认为我们的开发团队曾经有过这样的目的。如果我明确地想要在for循环中迭代共享一个变量,我将它声明在范围之外。 我想整个点在循环中声明一个变量是明确地限制它的范围到那个块。为了使这个编译时间错误将从语言中删除块级作用域。虽然方法级别范围是合理的,但是块级范围的重要性无疑也是可以的。我不认为你可以很容易地从语言中删除,而不用引入一些新的句法来使用它。在这一点上,你正在进入重新设计VB.NET的领域 - 我不确定有一个简单的方法来做到这一点。 Today I investigated a logical bug in our software and figured out that this is related to the way VB.NET thread variables inside a loop. Let's say I have the following code: produces the following output The problem is that In VB.NET, a variable that is declared inside a for loop keeps its value for the next itaration. This is by design: http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/c9cb4c22-d40b-49ff-b535-19d47e4db38d but this is also dangerous pitfall for programmers. However, until now, I haven't been aware of this problem/behaviour. Until now.
Most of our code base is C# anyway, which doesn't allow the use of an uninitialized variable, so there is no problem. But we have some legacy code that is written in VB.NET that we have to support. I don't think that anyone of our dev team has ever used this with purpose. If I explicitly want to share a variable over iterations inside a for loop, I declare it outside the scope. So the best thing would be to generate a warning or even an error in this specific case.
But even with Option Explicit / Option Strict this does not generate a warning / an error. Is there a way make this a compile time error or maybe a way to check this with FxCop? I don't think that anyone of our dev team has ever used this with purpose. If I explicitly want to share a variable over iterations inside a for loop, I declare it outside the scope. I suppose the whole point of declaring a variable inside the loop is to explicitly restrict its scope to that block, though. To make this a compile time error would remove block-level scope from the language. While there are certainly cases where method level scope is sensible there can no doubt also be a case made for the importance of block level scope. I don't think you can easily excise this from the language without introducing some new syntactic method for employing it. At this point you are entering the realm of redesigning VB.NET - I'm not sure that there is an easy way to do this. 这篇关于在for循环中声明的变量。我如何使这个编译时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我们的大部分代码都是C#,不允许使用未初始化的变量,所以没有问题。但是我们有一些在VB.NET中编写的遗留代码,我们必须支持。
但即使使用Option Explicit / Option Strict,也不会产生警告/错误。
Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5}
For Each number As Integer In numbers
Dim isEven As Boolean
If number Mod 2 = 0 Then
isEven = True
End If
If isEven Then
Console.WriteLine(number.ToString() & " is Even")
Else
Console.WriteLine(number.ToString() & " is Odd")
End If
Next
1 is Odd
2 is Even
3 is Even
4 is Even
5 is Even
isEven
is declared but not assigned.
In this specific case, it would be correct to write dim isEven as Boolean = false
but I haven't done this.