pass delegate as parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?emlubw==?=

    #1

    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
  • Jon Skeet [C# MVP]

    #2
    Re: pass delegate as parameter

    zino <zino@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 - <skeet@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

    Comment

    • =?Utf-8?B?emlubw==?=

      #3
      Re: pass delegate as parameter


      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 "


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: pass delegate as parameter

        zino <zino@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 - <skeet@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

        Comment

        • =?Utf-8?B?emlubw==?=

          #5
          Re: pass delegate as parameter

          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)

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: pass delegate as parameter

            zino <zino@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 - <skeet@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

            Comment

            • =?Utf-8?B?emlubw==?=

              #7
              Re: pass delegate as parameter

              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.




              Comment

              Working...