-2

I have a shared array that needs a calculation for each index. Right now, the array is initialized in New() and executes each time an object is created, which is redundant. How can I set it up so that the array is only initialized once, when it is created? Something like a static block in Java?

I didn't initially include the code because I thought this was just a simple question. The constructor is called 64 times (working with values of bits) and he relevant code is :

' Decimal value of each bit
Public Shared bitValue(63) As Long

Public Sub New()
    ' Other non-relevant code here.

    For index = 0 To 63
        bitValue(index) = 1L << index
    Next
End Sub

Just in case you are wondering what the heck I'm doing, I'm rewriting a Java program that I did that works with casting and bitwise operators. As part of learning VB, I'm working on a translation. You can get an idea of where it is headed by going to http://sourceforge.net/projects/javabitwise/.

Tom K
  • 321
  • 1
  • 4
  • 15
  • Show some code and some evidence that you researched this before posting here. – rory.ap Jul 13 '15 at 15:21
  • You may be confused. `Sub New()` executes only once when 'the object' whatever it is, is created. You cant call it again unless you create another new thing. So the array will only be initialized once. But, no code, so it is hard to say for sure – Ňɏssa Pøngjǣrdenlarp Jul 13 '15 at 15:22
  • roryapp, I did try looking for something similar in other posts, either there wasn't one or I didn't have the right keywords. But, apparently someone thinks I deserve a demerit for it (-1 at the moment). – Tom K Jul 13 '15 at 15:51
  • Plutonix, I just used a loop in the constructor and the array overwrites itself each time an object is instantiate. It works, but not efficient. – Tom K Jul 13 '15 at 16:00
  • Can anyone please explain why I'm getting down votes (now at -2)? This seems like a perfectly reasonable question. – Tom K Jul 13 '15 at 16:42

2 Answers2

2

You can initialize it right away

Class Test1

    Private Shared _list As New List(Of String) From {"1", "2"}

End Class

Or do it in a shared constructor

Class Test2

    Private Shared _list As List(Of String)

    Shared Sub New()
        _list = New List(Of String)
        _list.Add("1")
        _list.Add("2")
    End Sub

End Class

Or check if the value is initialized or not in the new

Class Test3

    Private Shared _list As List(Of String)

    Public Sub New()

        ' Use synclock
        If _list Is Nothing Then
            _list = New List(Of String)
            _list.Add("1")
            _list.Add("2")
        End If

    End Sub

End Class

Or put your list in a singleton

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • 1
    Dynamic array location is good idea. I didn't think of that. It doesn't work well on an array of 64 items, but it might be useful later. It looks like that the shared constructor is the ticket in this situation. Singleton? Don't know what that is yet. I'll try to look it up, but I might be back asking about it. I'm on a job, but on my own learning. Everyone else is too busy! lol – Tom K Jul 13 '15 at 16:04
  • I like the Shared Constructor approach. It will fire when you create an instance of the Class, or the first time you somehow access the Shared variable. – Idle_Mind Jul 13 '15 at 16:29
0

Here's a quick example of the Singleton approach. We access the class via its Instance() function:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Debug.Print(KibblesAndBits.Instance.bitValue(10))
    End Sub

End Class

Public Class KibblesAndBits

    Private Shared _Kibbles As KibblesAndBits = Nothing

    Public Shared Function Instance() As KibblesAndBits
        If IsNothing(_Kibbles) Then
            _Kibbles = New KibblesAndBits ' the private constructor will now fire and initialize
        End If
        Return _Kibbles
    End Function

    Public bitValue(63) As Long

    Private Sub New() ' Private Constructor means you can't instantiate it directly
        For index = 0 To bitValue.Length - 1
            bitValue(index) = index
        Next
    End Sub

    ' ... other standard Class code ...    

End Class
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40