0

There is the code:

Private Sub InsertItemInCache(Of T)(ByVal item As CachedItem(Of T), ByVal dependency As AggregateCacheDependency, _
 ByVal key As String, ByVal updateCallBack As CacheItemUpdateCallback)

The signature of CacheItemUpdateCallback is:

Sub CacheItemUpdateCallback(ByVal key As String, ByVal reason As CacheItemUpdateReason, _
     ByRef expensiveObject As Object, ByRef dependency As CacheDependency, ByRef absoluteExpiration As Date, _
     ByRef slidingExpiration As TimeSpan)

I want to call InsertItemInCache function using lamba expression for this. This code is not compiled:

InsertItemInCache(cachedItem, dependency, key, Function(k, r, e, d, a, s) CacheItemUpdateCallback(k, r, e, d, a, s))

it says Expression does not produce a value

If I change Sub CacheItemUpdateCallback to Function CacheItemUpdateCallback it also is not compiled and says Nested function does not have the same signature as delegate 'Delegate Sub CacheItemUpdateCallback(key As String, reason As System.Web.Caching.CacheItemUpdateReason, ByRef expensiveObject As Object, ByRef dependency As System.Web.Caching.CacheDependency, ByRef absoluteExpiration As Date, ByRef slidingExpiration As System.TimeSpan)'

Can anyone help me to call this method via lambda expression? I want to use closure in the future and call this function in such way:

InsertItemInCache(cachedItem, dependency, key, Function(k, r, e, d, a, s) CacheItemUpdateCallbackNew(k, r, e, d, a, s, additionalParameter1, additionalParameter2, additionalParameter3))
Egor4eg
  • 2,678
  • 1
  • 22
  • 41
  • Does this answer your question? [VB lambda expression for sub delegate](https://stackoverflow.com/questions/4302055/vb-lambda-expression-for-sub-delegate) – Sunny Patel Sep 04 '20 at 02:27

1 Answers1

2

How about this?

InsertItemInCache(cachedItem, dependency, key, _
  Sub(k, r, e, d, a, s) CacheItemUpdateCallback(k, r, e, d, a, s)) 

I think this will only work in VB.Net 2010. As far as I can remember earlier versions did not support Sub lambdas.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • 1
    @Egor4eg I think in VS2008 your only option is to rewrite the code so that you can use `Function` lambdas. Maybe write a wrapper `Function` that takes the same arguments as `CacheItemUpdateCallback`. It would call `CacheItemUpdateCallback` and then return `True`. You could also write a wrapper for `InsertItemInCache` that expects a delegate for the wrapper function. Then you could use `Function` lambdas. It's up to you whether this is worth it. – MarkJ Nov 30 '10 at 09:49
  • Thanks! I just want to make sure that VB syntax doesn't allow to do this in a simple way. I might use wrappers or something else. – Egor4eg Nov 30 '10 at 10:08