473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pass delegate as parameter

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
6 2827
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: yoramo | last post by:
hello can I pass a static method as a parameter to a method? if the answer is yes how do I do that ? how do I call the method ? yoramo.
4
by: Gopal Prabhakaran | last post by:
Pls Help me - Asap public void start(delegate wm,string name) - is this possiable ? if so Pls tell me how to do? r - give some sample codes? r - tell me some sites to learn?
4
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
6
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original project was written in VB. I have converted it to...
0
by: verpeter | last post by:
When try pass only one delegate as parameter, all work fine. But I need call function, that take as parameter array of function pointers. From C++ code I used such code: static void* aFuncPtrs =...
3
by: Bob Speaking | last post by:
Hi at all, Is possible to pass a parameter though a delegate or to override it? (I'm newbie and I'm trying to understand delegates and their use in a real scenario) In my scenario I need to...
3
by: John Dalberg | last post by:
I have seen examples for List<T>.FindAll(findthis)where findthis is a predicate. How do I pass a parameter to this predicate so that I have different values to search for? I don't want to use...
2
by: Curious | last post by:
I have a C#.NET program that uses a delegate, "BuildExistingReportFile". It's called in such a fashion: IList lFiles = this.GetListFromStoredProcedure( null,...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.