Multicast delegate confusion

This is a discussion on Multicast delegate confusion within the C# Programming forums, part of the General Programming Boards category; Consider the following code: Code: using System; using System.Collections.Generic; using System.Text; namespace DelegateTest { public delegate int Calc (string t); ...

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109

    Multicast delegate confusion

    Consider the following code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace DelegateTest {
    	
    	public delegate int Calc (string t);
    
    	public class Program {
    		static void Main(string[] args) {
    			Program t = new Program();
    			string st = "test";
    			
    			Calc c = new Calc(t.DoubleL);
    			c += new Calc(t.HalfL);
    			c += new Calc(t.Length);
    			
    			Console.WriteLine(c(st));   
    
    			c -= new Calc(t.Length);
    
    			Console.WriteLine(c(st));   
    
    			c -= new Calc(t.HalfL);
    
    			Console.WriteLine(c(st));   
    
    			Console.ReadLine();
    		}
    
    		private int DoubleL(string t) {
    			Console.WriteLine("DoubleL");
    			return t.Length * 2;
    		}
    
    		private int HalfL(string t) {
    			Console.WriteLine("HalfL");
    			return t.Length / 2;
    		}
    
    		private int Length(string t) {
    			Console.WriteLine("Length");
    			return t.Length;
    		}
    	}
    }
    Output is:

    DoubleL
    HalfL
    Length
    4
    DoubleL
    HalfL
    2
    DoubleL
    8
    Now my problem with this code.

    Everything does what it should, but how can I retrieve the 3 values being returned when i first call the delegate (since all 3 methods are being called when I call the delegate the first time ).

    So that I'll be able to have my 3 values at the first time the delegate is being called...

    I'm just wondering, since im learning for my C# exam this friday , I saw the += thing in my notes and was thinking how one should be able to get all those values at the same time.

    I hope someone understands what im trying to do here.

    Thanks in advance .
    Last edited by GanglyLamb; 06-07-2006 at 03:53 AM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You can't return multiple values from a delegate. As you've shown, you can only get the return value of the last called function. If you really want to get all the values, I'd suggest something like the following:
    Code:
    public delegate void Calc(string t, Collection<int> retVals);
    
    static void Main(string[] args)
    {
    	Program t = new Program();
    	string st = "test";
    	
    	Calc c = new Calc(t.DoubleL);
    	Collection<int> retVals = new Collection<int>();
    	c += new Calc(t.HalfL);
    	c += new Calc(t.Length);
    
    	c(st, retVals);
    	foreach(int i in retVals)
    		Console.WriteLine(i);   
    
    	Console.ReadLine();
    }
    
    private void DoubleL(string t, Collection<int> retVals) 
    {
    	Console.WriteLine("DoubleL");
    	retVals.Add(t.Length * 2);
    }
    
    private void HalfL(string t, Collection<int> retVals)
    {
    	Console.WriteLine("HalfL");
    	retVals.Add(t.Length / 2);
    }
    
    private void Length(string t, Collection<int> retVals)
    {
    	Console.WriteLine("Length");
    	retVals.Add(t.Length);
    }
    In general, relying on the return value of a delegate is a bad practice. If the delegate instance is not private, then there is the possibility that other users could hook up to the delegate, becoming the last in line and hence returning their value. If the delegate instance is private, then it becomes a code maintainence issue. It's very easy for someone else to come back around to your piece of code and subscribe a few more functions to your delegate without understanding that you need a specific return value.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Okido.

    Anyway I don't intend using a delegate this way , only time I probably use them is when I'm having several threads updating the same object or with events , which you can relate to updating an object.... most of the time I just use them to separate logic with my GUI.

    It was just a question out of curiosity ( since my notes didn't mention anything about getting multiple return values from it ).

    Thanks for replying pianorain.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2008, 12:39 PM
  2. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 01:59 AM
  3. [help need] IPv6 multicast in winsock2?
    By dudupig in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-30-2006, 06:10 PM
  4. Multicast Quiestion
    By arron in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-15-2005, 12:17 AM
  5. Sockets, multi home, multicast, security, complicated issue
    By ekymk in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-13-2004, 03:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21