473,659 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myDelegateRepor t(ByVal myInfoStore As
CrystalDecision s.Enterprise.In foStore) As
System.Collecti ons.Generic.Dic tionary(Of Long, String)

Public Shared Function GetCachedItem(B yVal itemKey As integer, ByVal funct
As myDelegateRepor t, ByVal myInfoStore As
CrystalDecision s.Enterprise.In foStore) As System.Web.Cach ing.Cache
If HttpRuntime.Cac he(itemKey) Is Nothing Then
Dim folder As System.Collecti ons.Generic.Dic tionary(Of Long, String) =
funct.Invoke(my InfoStore)
HttpRuntime.Cac he.Insert(itemK ey, folder, Nothing)
End If
Return HttpRuntime.Cac he
End Function

end class


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

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

Private Shared Function GetCachedObject (ByVal paramDelegate As
myDelegateInfoS tore, ByVal cacheKey As integer, ByVal myInfoStore As
InfoStore) As IDictionary
Dim myDelegate As New CacheFactory.my DelegateReport( AddressOf paramDelegate)
'error: AddressOf operand must the name of a method
....
end function
Private Shared Function GetFolderId(ByV al myInfoStore As InfoStore, ByVal
folderName As String) As Long
Dim folderList As Dictionary(Of Long, String) = GetCachedObject (AddressOf
GetAllFolders, CacheEnum.repor t_folders, myInfoStore)
....
End Function

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

end class

class testDelegate

sub test
dim id as integer= GetFolderId(myI nfoStore, "folderName ")
end sub
end class
Jan 2 '08 #1
6 2840
zino <zi**@noemail.n oemailwrote:
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.my DelegateReport( paramDelegate)

--
Jon Skeet - <sk***@pobox.co m>
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.Cac he.Insert(i, item, Nothing)
Return HttpRuntime.Cac he
end function
end class

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

private shared function GetCache(paramD elegate As myDel, item As
Integer) As Integer
dim myDelegate As New A.delCache(para mDelegate)
'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(i tem, myDelegate)
... ... .
end function
public shared function passFunct1() As Integer
return GetCache(Addres sOf 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 "paramDeleg ate"
(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.n oemailwrote:
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.co m>
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(para mDelegate)"
by
"Dim myDelegate As New A.delCache(CTyp e(paramDelegate , myDel))"

this does not work either (the same error message)
Jan 3 '08 #5
zino <zi**@noemail.n oemailwrote:
if you mean to replace
"Dim myDelegate As New A.delCache(para mDelegate)"
by
"Dim myDelegate As New A.delCache(CTyp e(paramDelegate , myDel))"

this does not work either (the same error message)
No,

Dim myDelegate As CType(paramDele gate, 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.co m>
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(para mDelegate) 'no need for this

Dim myCache As Cache = A.createCache(i tem, 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
21752
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
1950
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
2424
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
2399
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 C#. One of the problem is passing a function address to a COM function as a parameter with another progress value. My callback function is very simple using the progress value to update my progressbar. Because this COM function usually takes a long...
0
1466
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 = { &Function1, &Function2, &Function3, };
3
7176
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 override System.Text.RegularExpression.MatchEvaluator delegate passing it another parameter. For a concrete sample I paste some lines of code : //my regexpression pattern;
3
7338
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 global variables.
2
4207
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, Constants.StoredProcedures.SPcorReportInstanceFilesGet, new BuildDelegate(new ReportClosure(aReport).BuildExistingReportFile), new SqlParameter("@ReportInstanceID",aReport.ID));
24
55171
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 EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.