Variable declared inside a for loop. How do I make this to a compile time error?

后端 未结 3 1851
南方客
南方客 2020-12-16 03:04

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 follo

3条回答
  •  渐次进展
    2020-12-16 03:42

    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.

提交回复
热议问题