Delegates

Multicasting delegates

Page 1 of 1

2 Replies - 1839 Views - Last Post: 25 June 2007 - 05:44 AM Rate Topic: -----

#1 JustALearner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-June 07

Delegates

Post icon  Posted 07 June 2007 - 11:57 PM

hii all,



First time I am just learning Delegates in c#.



I have faced a conceptual problem.

In books it is written that A Multicasting Delegate should have a return type of Void unless it will throw a run time exception.

But i am written a code snippet with a Multicasting Delegate with a string return type .But it is not throw any exception at all.





Can anybody tell me what is happening here............



Plz somebody help me out...........



The code is given below:





<code>

delegate string Delegate_Multicast(int x, int y);

class Class2


{

static string Method1(int x, int y)

{

Console.WriteLine("You r in Method 1");

return "hi";

}

static string Method2(int x, int y)

{

Console.WriteLine("You r in Method 2");

return "hi";

}

public static void Main()

{

Delegate_Multicast func = new Delegate_Multicast(Method1);

func += new Delegate_Multicast(Method2);

string s=func(1,2); // Method1 and Method2 are called

}</code>

Is This A Good Question/Topic? 0
  • +

Replies To: Delegates

#2 psykoprogrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-October 06

Re: Delegates

Posted 13 June 2007 - 03:18 PM

With that code snippet you are likely getting errors due to trying to access the methods of that class that are private by default, and the fact that you are not referencing the method by its class. Try something like this.

using System;
using System.Collections.Generic;
using System.Text;

namespace test3
{
	class Program
	{
		delegate string Delegate_Multicast(int X, int Y);

		class Class2
		{
			static public string Method1(int X, int Y)
			{
				Console.WriteLine("u r in method1");
				return "Hello Method1";
			}

			static public string Method2(int X, int Y)
			{
				Console.WriteLine("u r in method2");
				return "Hello method2";
			}
		}

		static void Main(string[] args)
		{
			Delegate_Multicast func = new Delegate_Multicast(Class2.Method1);
			func += new Delegate_Multicast(Class2.Method2);

			string result = func(1, 2);
			Console.WriteLine("Complete.");
			

		}
	}
}


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Delegates

Posted 25 June 2007 - 05:44 AM

It seems pretty logical that there can't be a return value, since it is undefined which event's return value would be used; and also, if no events were attached, what would the return value then be? The C# compiler tends to not let you do things that might break your program...

For the pattern you are trying to implement, you probably want to take a different approach. Events are there to tackle specific types of problem, and have to be used differently if your problem is outside of that remit.

Instead, try creating a generic List<StringDelegate> where a StringDelegate has a string return type. Add a function that iterates through the list calling the delegates one by one, and returns the value you want at the end. Do you want the first string? The last one? Do you want them concatenating? Do you want to return an empty string, or just null, if the list has no delegates? Perhaps your StringDelegate should have a nullable return value, and then you can return the first non-null string? These things must all be defined, which is why the compiler won't let you do what you're trying.

-- serializer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1