在 for 循环中声明的变量.我如何使它成为编译时错误? [英] Variable declared inside a for loop. How do I make this to a compile time error?

查看:20
本文介绍了在 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.

假设我有以下代码:

    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 已声明但未分配.在这种特定情况下,将 dim isEven 编写为 Boolean = false 是正确的,但我没有这样做.

The problem is that 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.

在 VB.NET 中,在 for 循环中声明的变量会为下一次迭代保留其值.这是设计使然:http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/c9cb4c22-d40b-49ff-b535-19d47e4db38d 但这对程序员来说也是危险的陷阱.

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.

然而,直到现在,我还没有意识到这个问题/行为.到目前为止.无论如何,我们的大部分代码库都是 C#,它不允许使用未初始化的变量,因此没有问题.

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.

但是我们必须支持一些用 VB.NET 编写的遗留代码.

But we have some legacy code that is written in VB.NET that we have to support.

我认为我们开发团队中的任何人都没有故意使用过它.如果我明确想在 for 循环内的迭代中共享一个变量,我会在作用域之外声明它.

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.

因此,最好的办法是在这种特定情况下生成警告甚至错误.但即使使用 Option Explicit/Option Strict 这也不会产生警告/错误.

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.

有没有办法让它成为编译时错误,或者有没有办法用 FxCop 来检查这个?

Is there a way make this a compile time error or maybe a way to check this with FxCop?

推荐答案

我认为我们开发团队中的任何人都没有故意使用过它.如果我明确想在 for 循环内的迭代中共享一个变量,我会在作用域之外声明它.

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.

不过,我认为在循环内声明变量的全部意义在于将其范围明确限制为该块.要使其成为编译时错误,将从语言中删除块级作用域.虽然在某些情况下方法级别范围是合理的,但毫无疑问也可以说明块级别范围的重要性.我认为如果不引入一些新的句法方法来使用它,你就不能轻易地从语言中去除它.此时您正在进入重新设计 VB.NET 的领域 - 我不确定是否有一种简单的方法可以做到这一点.

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屋!

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