Click here to Skip to main content
15,903,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two variations of UpdateStatusLine:
public delegate void DelegateUpdateStatusLine(String text);
private DelegateUpdateStatusLine m_DelegateUpdateStatusLine;
public delegate void DelegateUpdateStatusLine(String text, int percentage);

private void UpdateStatusLine(String text)
	{ ... }
private void UpdateStatusLine(String text, int percentage)
	{ ... }


I need to pass the delegate to another class that will use both overloads. Passing either one is simple.
m_DelegateUpdateStatusLine = new DelegateUpdateStatusLine(this.UpdateStatusLine);
AnotherClass x = new AnotherClass(m_DelegateUpdateStatusLine);


How do I pass delegates that allow AnotherClass to use either form of UpdateStatusLine?
Posted

You can have both the overloaded methods in the same class. There is no restriction on that.
You can they simply call just the one you want!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-12 0:40am    
My 5. Weird question. Maybe the very inaccurate term "overload" is confusing. Bat term!
--SA
Abhinav S 20-Sep-12 1:16am    
Thank you SA.
Hey there,

Delegates are somewhat similar to Pointers in C. All they does is point at something and they don't contain logic. Therefore, it cannot point at 2 things at once and assuming it can, since it got no logic it wouldn't be able to figure out which one to call.

There are 2 approaches based on my opinion:
1. To create the overloaded method with default parameters and pass that method in the delegate. You have to write logic to see whether a certain parameter is assigned or it stay the default value.
2. You've figured out the way to do it for one method. Why not do it to the other overload as well and pass the same way? I.e. you'd have 2 parameters.

Hope this helps, regards
 
Share this answer
 
Comments
Roger500 20-Sep-12 4:01am    
In this case, the default parameter solution worked well. The integer being passed had a range of -1 to 100. I simply pass a -2 and the receiving routine ignores the integer parameter. Simple but effective. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900