MSDN文档错误? "如果程序是共享的,所有的局部变量都将自动共享。这包括静态变量&QUOT。 [英] MSDN documentation error? "If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables."

查看:157
本文介绍了MSDN文档错误? "如果程序是共享的,所有的局部变量都将自动共享。这包括静态变量&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在审查对VB.Net的很少使用的局部变量静态的关键字。我的问题是不可以有关如何静态工作(我个人打算避免使用它,因为它看起来像它可以使人忧愁未来的程序员谁也不会注意到的副作用。)我的问题是对本声明的文档

I was reviewing the MSDN documentation on VB.Net's little-used Static keyword for local variables. My question is not about how Static works (I personally plan to avoid using it as it seems like it could cause grief to future programmers who don't notice the side-effects.) My question is about this statement in the docs:

任何局部变量的行为取决于它是否被声明   在一个共享过程。如果程序是共享的,所有的地方   变量自动共享。这包括静态   变量。有这样一个可变的整个只有一个副本   应用程序。

The behavior of any local variable depends on whether it is declared in a Shared procedure. If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables. There is only one copy of such a variable for the whole application.

这似乎暗示,在一个共享子所有局部变量会表现得像静态变量 - 他们将继续在整个单独调用它们的值。但我知道这是不是这样的,我写了一个小的测试程序来证明这一点:

This seems to imply that all local variables in a Shared Sub would behave like Static variables -- they would keep their values across separate calls. But I knew this wasn't the case, and I wrote a little test program to prove it:

Class TestSharedSub

    Shared Sub Main()
        Test()
        Test()
        Test()
        Console.Write("Press any key to continue...") : Console.ReadKey()
        ' Output:
        ' 1, 1
        ' 2, 1
        ' 3, 1
    End Sub

    Shared Sub Test()
        Dim iNormal As Integer
        Static iStatic As Integer
        iNormal += 1
        iStatic += 1
        Console.WriteLine(iStatic & ", " & iNormal)
    End Sub

End Class

因此​​,谁能跨$ P $磅的方式,是有道理的上述声明给我吗?或者,这是文件中的错误?由于VS 2005版的文档中它已经存在并仍在$在Visual Studio 11版本p $ psent。

So can anyone interpret the above statement for me in a way that makes sense? Or is this a bug in the documentation? It's been there since the VS 2005 version of the docs and is still present in the Visual Studio 11 version.

推荐答案

没有,它是在胡说八道。它继续在未来一段被完全打破:

No, it's talking nonsense. It continues being utterly broken in the next paragraph:

如果程序不共享,它的局部变量的实例变量。

If the procedure is not Shared, its local variables are instance variables.

局部变量不是实例变量...

Local variables aren't instance variables...

谁写该页面就会出现一点也不了解当地的变量。天知道他们会做递归的东西。你要知道,它的开始了的合理的:

Whoever wrote that page appears not to understand local variables at all. Goodness knows what they'd make of recursion. Mind you, it starts off reasonably:

通常,在一个步骤的局部变量不再作为该过程结束后立即存在

Normally, a local variable in a procedure ceases to exist as soon as the procedure stops.

...但这种说法显然是矛盾的后来者。 叹息。

... but that statement is clearly in contradiction to the later ones. Sigh.

我建议你一个bug在连接

I suggest you file a bug on Connect.

这篇关于MSDN文档错误? "如果程序是共享的,所有的局部变量都将自动共享。这包括静态变量&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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