Delegates list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have method that should according an input argument (a collection) call
the appropriate callback delegate.

I am using a list, where I store a struct with fields: delegate, input
argument.

To invoke a proper delegate I have to go through this dictionary, compare
input argument and call delegate if arguments are match.

My question is:
1/ Is there any better way to do it? I don't feel very comfortable with this
solution.
2/ Another method has an argument delegate. This delegate should be removed
from the list and disposed. How will I find the correct delegate to delete?
There is Delegate.Equals method. Again, somhow it feels there should be a
more elegant solution.

Any idea to make it better? I can't use events to call appropriate delegates.

Thanks,
Lubomir
 
Lubomir said:
I have method that should according an input argument (a collection) call
the appropriate callback delegate.

I am using a list, where I store a struct with fields: delegate, input
argument.

To invoke a proper delegate I have to go through this dictionary, compare
input argument and call delegate if arguments are match.

My question is:
1/ Is there any better way to do it? I don't feel very comfortable with this
solution.

Why not have a dictionary from argument to delegate, and combine the
delegates as you go?
2/ Another method has an argument delegate. This delegate should be removed
from the list and disposed. How will I find the correct delegate to delete?
There is Delegate.Equals method. Again, somhow it feels there should be a
more elegant solution.

Delegate.Equals is the way to go here - although it'll be a lot easier
if you also have the argument for which you wish the delegate to be
removed. In the dictionary scenario above, that would then just be a
case of using:

dictionary[specifiedArgument] -= specifiedDelegate;

(That will use Delegate.Equals within Delegate.Remove.)
 
Thanks for answer.

The input argument is a list of possible "flags". The presense at least one
of this flags means I have to invoke a delegate that belongs to this set of
flags. So this input parameter is not suitable to be a key in a dictionary.

Lubomir


Jon Skeet said:
Lubomir said:
I have method that should according an input argument (a collection) call
the appropriate callback delegate.

I am using a list, where I store a struct with fields: delegate, input
argument.

To invoke a proper delegate I have to go through this dictionary, compare
input argument and call delegate if arguments are match.

My question is:
1/ Is there any better way to do it? I don't feel very comfortable with this
solution.

Why not have a dictionary from argument to delegate, and combine the
delegates as you go?
2/ Another method has an argument delegate. This delegate should be removed
from the list and disposed. How will I find the correct delegate to delete?
There is Delegate.Equals method. Again, somhow it feels there should be a
more elegant solution.

Delegate.Equals is the way to go here - although it'll be a lot easier
if you also have the argument for which you wish the delegate to be
removed. In the dictionary scenario above, that would then just be a
case of using:

dictionary[specifiedArgument] -= specifiedDelegate;

(That will use Delegate.Equals within Delegate.Remove.)
 
Lubomir said:
Thanks for answer.

The input argument is a list of possible "flags". The presense at least one
of this flags means I have to invoke a delegate that belongs to this set of
flags. So this input parameter is not suitable to be a key in a dictionary.

It sounds like each flag is a valid key for the dictionary though. When
you're then presented with a set of flags, just fetch the delegates
associated with each of the flags, combine them together, and you're
good to go.
 
Back
Top