Saturday, July 6, 2013

C Sharp - Delegates – Simple_example_of_Delegate_Function_Use


C Sharp - Delegates – Simple_example_of_Delegate_Function_Use

Extremely simple example:

output:






Code:
Program.cs:

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

namespace TestingStuff
{
    class Program
    {
        static void Main(string[] args)
        {


            ClassWithDelegate.WillReceiveDelegate(ThisWillConsoleOutTheMessage);

        
            Console.WriteLine("press enter...");
            Console.Read();
        }

        //Function that will be passed to a class that is set
        //up with a public variable to accept a function pointer/delegate:
        public static void ThisWillConsoleOutTheMessage(string TheMessage)
        {
            Console.WriteLine("TheMessage = " + TheMessage);
        }
    }
}
ClassWithDelegate.cs:

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

namespace TestingStuff
{
    public class ClassWithDelegate
    {
        //Define the Function Pointer/Delegate:   
        //  [Note: This has to be public]
        //  [Note: This only needs to be defined in the class using the pointer]
        public delegate void MyFunctionPointer(string sNow);
      
        //Method that accepts a Function Pointer from a call:
        public static void WillReceiveDelegate(MyFunctionPointer TheFunctionNow)
        {
            TheFunctionNow("\n This class does not have this function\n and yet it is using it now :)");
        }
    }
}





More complex example: 

output:



Code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.IO;

namespace MiscConsoleApp
{
    class Program
    {
        public delegate void Process_AnotherClass_Delegate(string DataNow);

        static void Main(string[] args)
        {
            Console.Clear();
            Console.WriteLine("");
            RunClassFunction_With_ClassInstance();
            RunClassFunction_WithOUT_ClassInstance();
            Console.WriteLine("");
        }

        private static void RunClassFunction_With_ClassInstance()
        {
            AnotherClass anotherClass = new AnotherClass();
            anotherClass.SendStringOutNow("Ok");
        }
        private static void RunClassFunction_WithOUT_ClassInstance()
        {
            AnotherClass anotherClass = new AnotherClass();
            WillRunWithoutClass(anotherClass.SendStringOutNow);
        }
        private static void WillRunWithoutClass(Process_AnotherClass_Delegate SentFunctionPointer)
        {
            //This function is using the function:
            //   public void SendStringOutNow(string WhatToSend)
            //of the "AnotherClass" class
            //but it does not have an instance of that class.
            SentFunctionPointer("I cannot believe this is really going out!");
        }
    }
    class AnotherClass
    {
        public AnotherClass() { }

        public void SendStringOutNow(string WhatToSend)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("WhatToSend = " + WhatToSend);
            Console.ResetColor();
        }
    }
}

No comments:

Post a Comment