开发者

Do local variables in shared method work like static variable in C?

开发者 https://www.devze.com 2023-03-29 23:10 出处:网络
Will the list in this shared method keep its state throughout the life of the method? Or will a new list be created every time this method is called?

Will the list in this shared method keep its state throughout the life of the method? Or will a new list be created every time this method is called?

Protected Shared Function newResxNodes(ByVal newName As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode)

    Dim newResxNodesList As List(Of ResXDataNode) = New List(Of ResXDataNode)

    Dim newResxNode As ResXDataNode = New ResXDataNode(newName, newValue)
    If newComment <> String.Empty T开发者_运维技巧hen
        newResxNode.Comment = newComment
    End If

    newResxNodesList.Add(newResxNode)

    Return newResxNodesList
End Function


No, It does not work like static variables in C. It will be a new list for every call. If you want to retain the list and list items, create a shared class field.


I've done a test and it returns 3 lines.

Module Module1

Class b

    Public Sub New()
        Console.WriteLine("New")
    End Sub

End Class

Class a

    Public Shared Sub Test()

        Dim c As b = New b

    End Sub

End Class

Sub Main()

    a.Test()
    a.Test()
    a.Test()

    Console.ReadLine()

End Sub

End Module
0

精彩评论

暂无评论...
验证码 换一张
取 消