// Multicast.cpp : main project file.

#include "stdafx.h"

using namespace System;
delegate void NotifyDelegate(int);

ref class Client1
{
	public:
		static void NotifyFunction1(int n)
		{
			Console::WriteLine(L"Client1: got value {0}", (Object^)(n));
		}
};

ref class Client2
{
	public:
		static void NotifyFunction2(int n)
		{
			Console::WriteLine(L"Client2: got value {0}", (Object^)(n));
		}
};

int main(array<System::String ^> ^args)
{
	Console::WriteLine(L"Multicast Delegates");

	// Declare two delegates
	NotifyDelegate ^pDel1, ^pDel2;
	
	// Bind them to the functions
	pDel1 = gcnew NotifyDelegate(&Client1::NotifyFunction1);
	pDel2 = gcnew NotifyDelegate(&Client2::NotifyFunction2);

	// Combine the invocation lists of the two delegates
	NotifyDelegate ^pDel3 = 
		dynamic_cast<NotifyDelegate^>(Delegate::Combine(pDel1, pDel2));

	// Invoke the multicast delegate
	Console::WriteLine(L"Invoking pDel3");
	pDel3->Invoke(3);

	// Create a second multicast delegate and invoke it
	NotifyDelegate ^pDel4 = 
		dynamic_cast<NotifyDelegate^>(Delegate::Combine(pDel3, pDel3));
	Console::WriteLine(L"Invoking pDel4");
	pDel4->Invoke(3);

	// Remove an item
	NotifyDelegate ^pDel5 = 
		dynamic_cast<NotifyDelegate^>(Delegate::Remove(pDel3, pDel1));
	Console::WriteLine(L"Invoking pDel5");
	pDel5->Invoke(3);

    return 0;
}

