Delegate to call a function

R

RP

I have a function to fill combo box items. I pass SQL query and combo
box name as arguments. This function is located in another class. In
my Form, I have declared a delegate to this function before the
namespace as;

public delegate void dFillItems(ObjectName as string, Query as string)

On my Forms I have several combo boxes and get filled when they get
focus. Is it proper to use delegate in this situation? Will it improve
performance? In what situations to use delegate?
 
P

Peter Duniho

[...]
On my Forms I have several combo boxes and get filled when they get
focus. Is it proper to use delegate in this situation? Will it improve
performance? In what situations to use delegate?

You'd use a delegate any time that you want to be able to write
general-purpose code that calls some method that cannot or should not be
specified except at run-time. As compared to specifying the method to be
called explicitly where the call is made, using a delegate won't improve
performance and in fact probably will reduce performance by a very tiny
amount (it's unlikely to be by an amount you'd be able to measure in a
real-world application).

As far as what's proper to use in the situation you describe, that's hard
to say. You didn't describe the situation in much detail, nor even
exactly how you're using the delegate. If you're storing the delegate
explicitly, then at the very least it's possible your code would work a
little better from a design standpoint if you used an event instead:
instead of assigning a delegate to some variable, you'd add it to the
event as a subscriber. But this is more a matter of writing the code so
that the design conforms more to the standard mechanisms used in
..NET...it's not something that would affect performance or even the basic
functionality.

Another alternative would be to define an interface that requires the
method you're going to call. Then the class that implements the method
would just be declared as implementing that interface. Rather than
storing a delegate in this case, you'd store the reference to an instance
of that class and call the interface method directly. Obviously, this
would only work if the method isn't static.

And of course, a third alternative is to just make the code that calls the
method dependent on the class that declares it. In this case, you would
simply store the reference to an instance of that class and call the
method as needed. IMHO, this would be fine if your code is such that you
will always be calling this particular class. The main reason for
abstracting things away from something like this is to make the code more
general-purpose, but that's not always a design requirement.

Given your question, there's no good way for anyone to tell you with
certainty what the "best" approach would be. But hopefully based on your
own knowledge of your code and the above, you can figure that out yourself.

Pete
 
R

RP

As far as what's proper to use in the situation you describe, that's hard  
to say.  You didn't describe the situation in much detail, nor even  
exactly how you're using the delegate.

I am using Delegate on Combo Box Enter Event as follows:

Delegate D1 = new Delegate();
D1.Invike(dFillItems(ThisCombo, strQuery):
 
P

Peter Duniho

I am using Delegate on Combo Box Enter Event as follows:

Delegate D1 = new Delegate();
D1.Invike(dFillItems(ThisCombo, strQuery):

Somehow, I have this little hunch that's not exactly the code you're using.
 
R

RP

Peter,

I think I missed something. I have that code in my office and today is
sunday. I remember that much only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top