user4951
user4951

Reputation: 33080

How to run a code once in vb.net for a class?

Basically I have a class with a static shared member. I want a code that run once to initiate that static shared member.

Class algorithm
    Public name As String
    Public lowPriceatNiceHash As Double
    Public highPriceatNiceHash As Double
    Public fixedPriceatNicehash As Double

    Shared dictOfAlgorithm As Generic.Dictionary(Of String, algorithm)
End Class

Now that dictOfAlgorithm dictionary should be set to a new empty one at a start of the use of the class. One and only one time.

Upvotes: 0

Views: 470

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

Initialize it where its declared:

Class ...

    Public Shared dictOfAlgorithm As Generic.Dictionary(Of String, algorithm) = New Generic.Dictionary(Of String, algorithm)

End Class

or use the .cctor:

Class ...

    '' Shared .ctor, called once the first time the class is accessed.
    Shared Sub New()
        dictOfAlgorithm = New Generic.Dictionary(Of String, algorithm)
    End Sub

    Public Shared dictOfAlgorithm As Generic.Dictionary(Of String, algorithm)

End Class

Upvotes: 1

Related Questions