Results 1 to 5 of 5

Thread: Trying to refactor this code and hit a wall. [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    Trying to refactor this code and hit a wall. [resolved]

    Here is a sample of the code I am refactoring it works great as is, but I don't want to write (copy/paste/change) this code for every time I need it.

    VB Code:
    1. Public Sub RaiseSendMessageEvent(ByVal e As DocGenCommon.DocGen.DocGenEventArgs)
    2.  
    3.         For Each del As [Delegate] In SendMessageEvent.GetInvocationList()
    4.             Try
    5.                 del.Method.Invoke(del.Target, New Object() {e})
    6.             Catch
    7.                 SendMessageEvent = DirectCast(System.Delegate.Remove(SendMessageEvent, del), IDocGenEventServer.SendMessageHandler)
    8.             End Try
    9.         Next
    10.  
    11.     End Sub

    So far I have gotten this far and my problem is I cannot pass in the Type to the directcast statement.

    VB Code:
    1. Public Sub RaiseRemotedEvent(ByVal sender As [Delegate], ByVal eventtype As Type, ByVal e As DocGenCommon.DocGen.DocGenEventArgs)
    2.  
    3.         For Each del As [Delegate] In sender.GetInvocationList()
    4.             Try
    5.                 del.Method.Invoke(del.Target, New Object() {e})
    6.             Catch
    7.                 ' this is where the error is "Type 'eventtype' is not defined."
    8.                 sender = DirectCast(System.Delegate.Remove(sender, del), eventtype)
    9.             End Try
    10.         Next
    11.  
    12.     End Sub

    Any suggestions?
    Last edited by TType85; Jun 16th, 2005 at 02:00 PM.
    Chris Wilson

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Trying to refactor this code and hit a wall.

    try ...., eventtype.gettype) instead...
    that parameter is expecting a type not a variable name.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    Re: Trying to refactor this code and hit a wall.

    Quote Originally Posted by Andy
    try ...., eventtype.gettype) instead...
    that parameter is expecting a type not a variable name.
    Tried that.

    I've tried eventtype.GetType, GetType(eventtype), passing an object declaring a type and using GetType. So far nothing works.

    I should rewrite this in C# and see if it complains.
    Chris Wilson

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    Re: Trying to refactor this code and hit a wall.

    It appears to work if I use this:

    VB Code:
    1. sender = System.Delegate.Remove(sender, del)

    I will have to do more testing to make sure it works right.
    Chris Wilson

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    Re: Trying to refactor this code and hit a wall.

    This is what I found that worked.

    VB Code:
    1. Public Sub RaiseRemotedEvent(ByVal sender As [Delegate])
    2.  
    3.         RaiseRemotedEvent(sender, New Object() {})
    4.  
    5.     End Sub
    6.  
    7.     Public Sub RaiseRemotedEvent(ByVal sender As [Delegate], ByVal args As Object())
    8.  
    9.         For Each del As [Delegate] In sender.GetInvocationList()
    10.  
    11.             Try
    12.                 del.Method.Invoke(del.Target, args)
    13.             Catch
    14.                 sender = System.Delegate.Remove(sender, del)
    15.             End Try
    16.  
    17.         Next
    18.  
    19.     End Sub
    Chris Wilson

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width