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

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

问题描述

我正在查看VB.Net很少使用的静态关键字,用于局部变量.我的问题不是不是关于Static的工作方式(我个人打算避免使用它,因为它可能会给未注意到副作用的未来程序员带来痛苦.)我的问题关于文档中的此声明:

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:

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

这似乎暗示Shared Sub中的所有局部变量的行为都类似于静态变量-它们将在单独的调用中保留其值.但是我知道事实并非如此,我编写了一个小测试程序来证明这一点:

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

那么任何人都可以以一种有意义的方式为我解释以上陈述吗?还是这是文档中的错误?自VS 2005版本以来,它就一直存在,并且仍存在于Visual Studio 11版本中.

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.

我建议您在连接上提交错误.

I suggest you file a bug on Connect.

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

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