HighTechTalks DotNet Forums  

pass delegate as parameter

Dotnet General Discussions microsoft.public.dotnet.general


Discuss pass delegate as parameter in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
zino
 
Posts: n/a

Default pass delegate as parameter - 01-02-2008 , 02:40 PM






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

Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: pass delegate as parameter - 01-02-2008 , 03:38 PM






zino <zino (AT) noemail (DOT) noemail> wrote:
Quote:
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 - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
zino
 
Posts: n/a

Default Re: pass delegate as parameter - 01-03-2008 , 02:03 PM




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"



Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: pass delegate as parameter - 01-03-2008 , 02:23 PM



zino <zino (AT) noemail (DOT) noemail> wrote:
Quote:
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 - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #5  
Old   
zino
 
Posts: n/a

Default Re: pass delegate as parameter - 01-03-2008 , 02:33 PM



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)

Reply With Quote
  #6  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: pass delegate as parameter - 01-03-2008 , 02:47 PM



zino <zino (AT) noemail (DOT) noemail> wrote:
Quote:
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 - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.