pass delegate as parameter

Z

zino

I'm trying to pass a delegate as parameter but the code below does not
compile. It display an error: 'AddressOf operand must the name of a
method(without parantheses)'
How can I make it work.

Public Class CacheFactory
Delegate Function myDelegateReport(ByVal myInfoStore As
CrystalDecisions.Enterprise.InfoStore) As
System.Collections.Generic.Dictionary(Of Long, String)

Public Shared Function GetCachedItem(ByVal itemKey As integer, ByVal funct
As myDelegateReport, ByVal myInfoStore As
CrystalDecisions.Enterprise.InfoStore) As System.Web.Caching.Cache
If HttpRuntime.Cache(itemKey) Is Nothing Then
Dim folder As System.Collections.Generic.Dictionary(Of Long, String) =
funct.Invoke(myInfoStore)
HttpRuntime.Cache.Insert(itemKey, folder, Nothing)
End If
Return HttpRuntime.Cache
End Function

end class




I created a utilities class that retrieve/create the cache:

Public Class Utilities
Private Delegate Function myDelegateInfoStore(ByVal myInfoStore As
InfoStore) As Dictionary(Of Long, String)

Private Shared Function GetCachedObject(ByVal paramDelegate As
myDelegateInfoStore, ByVal cacheKey As integer, ByVal myInfoStore As
InfoStore) As IDictionary
Dim myDelegate As New CacheFactory.myDelegateReport(AddressOf paramDelegate)
'error: AddressOf operand must the name of a method
....
end function


Private Shared Function GetFolderId(ByVal myInfoStore As InfoStore, ByVal
folderName As String) As Long
Dim folderList As Dictionary(Of Long, String) = GetCachedObject(AddressOf
GetAllFolders, CacheEnum.report_folders, myInfoStore)
....
End Function

Private Shared Function GetAllFolders(ByVal myInfoStore As InfoStore) As
Dictionary(Of Long, String)
.....
end function

end class



class testDelegate

sub test
dim id as integer= GetFolderId(myInfoStore, "folderName")
end sub
end class
 
J

Jon Skeet [C# MVP]

zino said:
I'm trying to pass a delegate as parameter but the code below does not
compile. It display an error: 'AddressOf operand must the name of a
method(without parantheses)'
How can I make it work.

Don't use AddressOf - that's used to convert a method name into a
delegate instance. In this case (as far as I can understand the code -
improving the formatting would help a great deal in terms of
readability) you already *have* a delegate instance, so just pass it
directly:

Dim myDelegate As New CacheFactory.myDelegateReport(paramDelegate)
 
Z

zino

I tried without "AddressOf" but it gave this error:
" 'A.delCache' is a delegate type and requires a single 'AddressOf'
expression as the only argument to the constructor"

I could not formatted better due to the long naming characaters, but here is
a shortcut (code below is incomplete but tried to make it clear about I'm
trying to do):


class A
delegate function delCache() As Integer

public shared function createCache(i As Integer, funct As delCache) as
cache
dim item = funct.Invoke()
HttpRuntime.Cache.Insert(i, item, Nothing)
Return HttpRuntime.Cache
end function
end class



'a utilities class to retrieve the cache:
class B
private delegate function myDel() As Integer

private shared function GetCache(paramDelegate As myDel, item As
Integer) As Integer
dim myDelegate As New A.delCache(paramDelegate)
'ERROR: 'A.delCache' is a delegate type and requires a single
'AddressOf' expression as the only argument to the constructor"

dim myCache As Cache = A.createCache(item, myDelegate)
... ... .
end function


public shared function passFunct1() As Integer
return GetCache(AddressOf myFunct1, 1)
end function

private shared function myFunct1() As Integer
return 1
end Function

end class


'flow :
'1- a client would call function: "passFunct1"
'2- "passFunct1" somehow need to pass "myFunct1" as parameter to "GetCache"
'3- "GetCache" in turn, somehow need to pass the parameter "paramDelegate"
(which is basically "myFunct1") to class "A.delCache"

The problem is I don't know how to make "passFunct1" pass "myFunct1" to
"GetCache" and then make "GetCache" pass "myFunct1" in turn to "A.delCache"
 
J

Jon Skeet [C# MVP]

zino said:
I tried without "AddressOf" but it gave this error:
" 'A.delCache' is a delegate type and requires a single 'AddressOf'
expression as the only argument to the constructor"

Rats - sorry, that would have worked in C#.

Does CType work, perhaps?
 
Z

zino

if you mean to replace
"Dim myDelegate As New A.delCache(paramDelegate)"
by
"Dim myDelegate As New A.delCache(CType(paramDelegate, myDel))"

this does not work either (the same error message)
 
J

Jon Skeet [C# MVP]

zino said:
if you mean to replace
"Dim myDelegate As New A.delCache(paramDelegate)"
by
"Dim myDelegate As New A.delCache(CType(paramDelegate, myDel))"

this does not work either (the same error message)

No,

Dim myDelegate As CType(paramDelegate, myDel)

I'm not really a VB person, but I'd hope there's some way of converting
an existing delegate instance into one of a different but compatible
type in VB...
 
Z

zino

I solved it and here is how:


'''''''''' Private Delegate Function myDel() As Integer 'no need for this
delegate

and I declared the passed parameter as the delegate type:
'paramDelegate As A.delCache


Private Shared Function GetCache(ByVal paramDelegate As A.delCache, ByVal
item As Integer) As Integer
''''''''''Dim myDelegate As New A.delCache(paramDelegate) 'no need for this

Dim myCache As Cache = A.createCache(item, paramDelegate)
End Function


I appreciate your help, and thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top