0

If I use in VB.NET a property get of this shape:

Public ReadOnly Property Thing(i as long) as double(,)
    Get
        Return SomeCalculation(i )
    End Get
End Property

and the code calls many times the property get with the same i (after doing the same with another iscen and so on), will the result be cached until I use a new i or will it be recalculated each time?

Thanks!

Pierre
  • 1,046
  • 7
  • 21
  • 2
    It will be recalculated each time. There is no automatic provision of caching. – Steve Dec 30 '16 at 14:01
  • A property is simply syntactic sugar and is implemented as one or a pair of methods. Method results do not get implicitly cached. I'd say that that should probably not even be a property but rather a method, in which case there would be no confusion. – jmcilhinney Dec 30 '16 at 14:02
  • Thanks to both. Will you put it as an answer so the question is marked as answered? – Pierre Dec 30 '16 at 14:04

2 Answers2

1

No there is no automatic caching in VB.NET to store the result of repeated calculations. It is up to you to provide some kind of caching.

For example you can use a Dictionary

 Dim cache As Dictionary(Of Long, Double(,)) = New Dictionary(Of Long, Double(,))

Public ReadOnly Property Thing(i as long) as double(,)
    Get
       Dim result As Double(,)
       If Not cache.TryGetValue(i, result) Then
           result = SomeCalculation(i)
          cache.Add(i, result)
       End If
       Return result
   End Get
End Property

Of course,as any simple solution, there are some points to consider:

  • There is no invalidation rule (the cache remains active for the lifetime of the app)
  • It assumes that the calculation produces always the same result for the same input
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Also consider using `Lazy`. – Cody Gray - on strike Dec 30 '16 at 14:16
  • @CodyGray interesting. I am not used to it. Could you provide an example? – Steve Dec 30 '16 at 14:18
  • 1
    Declare a private backing variable of type `Lazy`, and then create a public property that returns the `Value` member of the `Lazy` variable. See, for example: http://stackoverflow.com/a/2579378/366904 and http://stackoverflow.com/questions/5134786/cached-property-vs-lazyt – Cody Gray - on strike Dec 30 '16 at 14:21
  • Very nice answer. I have written another answer as an alternative, but it might have some syntax errors, as it was not tested. Can you take a look? – Lajos Arpad Dec 30 '16 at 15:00
1

You could create a class for cached values

Public Class LongCaches(Of MyType)
     Protected MyDictionary Dictionary(Of Long, MyType) = New Dictionary(Of Long, MyType)
     Public Delegate Function MyFunction(Of Long) As MyType
     Protected MyDelegate As MyFunction
     Public Calculate As Function(ByVal input As Long) As MyType
         If Not MyDictionary.ContainsKey(input) Then
             MyDictionary(input) = MyFunction.Invoke(input)
         End If
         Return MyDictionary(input)
     End Function
     Public Sub New(ByVal myfunc As MyFunction)
         MyDelegate = myfunc
     End Sub
End Caches

You will need to use it like this:

Private _MyLongCacheProperty As LongCaches(Of Double(,))
Protected ReadOnly MyLongCacheProperty(i As Long) As LongCaches
Get
    If _MyLongCacheProperty Is Nothing Then
        _MyLongCacheProperty = New LongCaches(Of Double(,))(AddressOf SomeCalculation)
    End If
    Return _MyLongCacheProperty.Calculate(i)
End Get
End Property

Notice: This code is untested, if there are syntax errors, then please comment or edit rather than downvote.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175