Are static local variables bad practice?

前端 未结 2 336
遥遥无期
遥遥无期 2021-01-18 00:39

Related C++ question: Static local variables in methods a bad practice?

In VB.NET, when I want a simple counter or something that increments each time a method is ca

2条回答
  •  抹茶落季
    2021-01-18 01:23

    I would NOT recommend this.

    Static in Visual Basic means that one or more declared local variables are to continue to exist and retain their latest values after termination of the procedure in which they are declared. Reference: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/static

    So, why would you do that? Next time you come into this Sub you will anyways reinitialize this variable. I don't think you can even access it anymore, unless you would have a second instance of this class, and if both instances run at the same time the value of "a" could affect the value of the "a" in the second instance. Unless intended, that would be disastrous. As the previous answer stated correctly - the smaller the scope the better.

    So, unless I am mistaken, this would be a very bad practice.

提交回复
热议问题