// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#using <mscorlib.dll>

using namespace System;

delegate void Process(int v);

ref class Doer
{
    public:
        int i;
        Process^ p1;
        Process^ p2;
        Process^ p3;
        Process^ p4;
        Process^ p5;
        Process^ p6;
        void ProcessAll();
        void InstantP(int v);
        static void ClassP(int v);
};

ref class Other
{
    public:
        void InstantP(int v);
        static void ClassP(int v);
};

void Doer::ProcessAll()
{
    p1(i);
    p2(i);
    p3(i);
    p4(i);
    p5(i);
    p6(i);
}

void Doer::InstantP(int v)
{
    Console::WriteLine("Doer::InstantP v=" + v + " (i=" + i +")");
}

void Doer::ClassP(int v)
{
    Console::WriteLine("Doer::ClassP v=" + v);
}

void Other::InstantP(int v)
{
    Console::WriteLine("Other::InstantP v=" + v);
}

void Other::ClassP(int v)
{
    Console::WriteLine("Other::ClassP v=" + v);
}

void OldStyle(int v)
{
    Console::WriteLine("OldStyle v=" + v);
}

int main()
{
    Doer^ a = gcnew Doer();
    a->i = 123;
    a->p1 = gcnew Process(&Doer::ClassP);
    a->p2 = gcnew Process(a, &Doer::InstantP);
    Doer^ a2 = gcnew Doer();
    a2->i = 456;
    a->p3 = gcnew Process(a2, &Doer::InstantP);
    Other^ b = gcnew Other();
    a->p4 = gcnew Process(&Other::ClassP);
    a->p5 = gcnew Process(b, &Other::InstantP);
    a->p6 = gcnew Process(&OldStyle);
    a->ProcessAll();
    return 0;
}
