By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

pass delegate as parameter

P: n/a
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
Jan 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
zino <zi**@noemail.noemailwrote:
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)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 2 '08 #2

P: n/a

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"
Jan 3 '08 #3

P: n/a
zino <zi**@noemail.noemailwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 3 '08 #4

P: n/a
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)
Jan 3 '08 #5

P: n/a
zino <zi**@noemail.noemailwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 3 '08 #6

P: n/a
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.


Jan 4 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.