Example #1
0
        static void Main(string[] args)
        {
            //    PropertyClass cl1 = new PropertyClass();
            //    cl1.Event1 += new EventHandler3(Method1);
            //    cl1.Event1 -= new EventHandler3(Method1);
            //    cl1.Event1 += new EventHandler3(Method1);

            //    cl1.RaiseEvent1(10);

            //    cl1.Event2 += new EventHandler4(Method2);
            //    cl1.Event2 += new EventHandler4(Method2);
            //    cl1.Event2 -= new EventHandler4(Method2);

            //    cl1.RaiseEvent2("arif");
            //    PropertyClass1 cl2 = new PropertyClass1();
            //    cl2.Event1 += new EventHandler3(Method1);
            //    cl2.RaiseEvent1(10);

            //    my = new MyEvent();
            //    my.Event1 += new FNamehandler(my_Event1);
            //    Program p = new Program();
            //    p.Load(my);
            //    my.RaiseFNameEvent("Arif", "Khan", "Hasan");

            //GenericDelegate gen = new GenericDelegate();
            //gen.EventUseOf1 += new Mydel<EventHandler1>(gen_EventUseOf1);
            //gen.raiseEvent(10);
            GenericDelegate<EventArgs> gen = new GenericDelegate<EventArgs>();
            gen.GEvent += new GenericDelegate<EventArgs>.Mydel1<EventArgs>(gen_GEvent);
        }
Example #2
0
        static void Main(string[] args)
        {
            //var delegateInt = new DelegateInt(addInt);
            //var delegateResult = delegateInt(5,6);

            var doubleDelegate = new GenericDelegate<double>(addDouble);
            var intDelegate = new GenericDelegate<int>(addInt);
        }
Example #3
0
 public RepeatingFunctionTask(GenericDelegate <bool> function)
 {
     mFunction = function;
     mDelay    = 500;
 }
 public static TException ExpectException <TException>(GenericDelegate del) where TException : Exception
 {
     return(ExpectException <TException>(del, false));
 }
 /// <summary>
 /// Redefine needed so that we can construct and pass anonymous delegates with parater.
 /// No need to specify the Value parameter(s), since they can be recognized automatically by compiler.
 /// </summary>
 public static void FireAndForget <ValueType1, ValueType2, ValueType3>(GenericDelegate <ValueType1, ValueType2, ValueType3> d, params object[] args)
 {
     _threadPoolEx.Queue(d, args);
 }
 public static ArgumentException ExpectArgumentExceptionNullOrEmpty(GenericDelegate del, string paramName)
 {
     return(ExpectArgumentException(del, "Value cannot be null or empty.\r\nParameter name: " + paramName));
 }
 private static TException ExpectExceptionHelper <TException>(GenericDelegate del) where TException : Exception
 {
     return(ExpectExceptionHelper <TException>(del, false));
 }
 public OpCodeFunction(Type type, GenericDelegate operation, bool global = false)
 {
     this.type      = type;
     this.operation = operation;
     this.global    = global;
 }
 public static ArgumentException ExpectArgumentException(GenericDelegate del, string exceptionMessage, string paramName)
 {
     return(ExpectArgumentException(del, exceptionMessage + "\r\nParameter name: " + paramName));
 }
Example #10
0
        private void BuildGenericDelegate()
        {
            var inputType = subGraphInput.DataType.UnderlyingType;

            genericDelegate = new GenericDelegate <Func <object, object, object, Task <bool> > >(this, EvaluateInternalAttribute.GetMethod(GetType()));
        }
Example #11
0
 public RepeatingFunctionTask(GenericDelegate <bool> function, int delay)
 {
     mFunction = function;
     mDelay    = delay;
 }
Example #12
0
 public static void AddReward(GenericDelegate d)
 {
     q.Enqueue(d);
 }
 public void UseDelegate(GenericDelegate del)
 {
 }
        public void TestGetNum()
        {
            int expectNum = GenericDelegate.getNum();

            Assert.AreEqual(10, expectNum);
        }
        static void Main(string[] args)
        {
            #region BONOUS STUFF DEMO
#if BONOUS
            //method to return multiple values from a function
            double a = 9.0, square, squareRoot;
            double b = 3.0, squareB, squareRootB;
            double c = 12.0, squareC, squareRootC;

            SquareAndRoot(a, out square, out squareRoot);
            SquareAndRoot(b, out squareB, out squareRootB);
            SquareAndRoot(c, out squareC, out squareRootC);

            Console.WriteLine("Square of the number {0} = {1} and its Square Root = {2}", a, square, squareRoot);
            Console.WriteLine("Square of the number {0} = {1} and its Square Root = {2}", b, squareB, squareRootB);
            Console.WriteLine("Square of the number {0} = {1} and its Square Root = {2}", c, squareC, squareRootC);

            //method to calculate average of n Numbers
            int numberOfParams = 0;
            Console.Write("Enter number of things you would like take average of : ");
            while (!Int32.TryParse(Console.ReadLine(), out numberOfParams))
            {
                Console.Write("Enter A number please : ");
            }
            GetN_Numbers(numberOfParams);
#endif
            #endregion

            /*--------------------------------------------------------------------------------------------------------*/

            #region ALL TYPES OF EVENT HANDELING

            #region  Events Discription
            /// ///////////////////////////////////////////////////////////////
            //            Events Discription
            /// ///////////////////////////////////////////////////////////////
            /// Steps for creating an event
            ///  There are 4
            /// . Declare an event
            /// . Create custom event argument class if needed
            /// . Raise the event
            /// . Write a default event handler.
            ///
            /// Data type of an event handler is delegate( System.EventHandler )
            ///   And event args should be EventArgs or one of its is child class.
            ///   PriceChanged(source, new EventArgs());
            /// ///////////////////////////////////////////////////////////////
            #endregion

            #region EVENT HANDLING PRO WAY DEMO WITH 4 STEPS
#if EVENT_HANDLING_WITH_4_STEPS_DEMO
            /// /////////////////////////////////////////////////////////
            //      WE HAVE 4 STEPS IN THIS WAY
            /// ////////////////////////////////////////////
            /// 1. DECLARE AN EVERNT
            /// 2. CREATE CUSTOM EVENT ARGUMENT CLASS IF NEDDED and that class will inherit from EventArgs or any of its Child class
            /// (this is required when you want to make it do things your way).
            /// 3. RAIS THE EVENT
            /// 4. WRITE A DEFAULT EVENT HANDLER (this is only required if you some how don't want to handle the evernt)
            ///
            ///  For this example we will create
            ///  a. A PERSON class
            ///  b. With Two even nameChangedEvent and Name ChangingEvent
            ///  c. A NameChangingEvent Args Class
            ///  d. default event handlers
            ///  e. we'll  raise the event and  then handle the event
            ///
            /// Note : We'll use Builtin EventHandler (its a builtin generic delegate used to handle events)
            /// //////////////////////////////////////////////////////////

            // create object of any class that has events

            Person person = new Person();

            //Step 5 : handle the events here

            // i'm lazy to do that long shit
            person.NameChangedEvent += (sender, eventArgs) => {
                Console.WriteLine(" Name Changed ");
            };
            person.NameChangingEvent += (s, e) => {
                Console.WriteLine(" {0} ----> {1} ", e.OldName, e.NewName);
            };

            person.Name = "Hassan";
            person.Name = "Salman";
            person.Name = "Waseen";
            person.Name = "Usman";
            person.Name = "Ahmer";
#endif
            #endregion

            #region LOCAL EVENT HANDLING DEMO
#if LOCAL_EVENT_HANDLING_DEMO
            // create an object of the cals with the event
            EventClas myEvt = new EventClas();

            // subscribe to the event
            myEvt.valueChanged += new myEventHandlerDele(MyEvtClas_valueChanged);

            // we can also do something like this
            //myEvt.valueChanged += (sender) => {
            //    Console.WriteLine("Lambda Handle ->> {0}", sender);
            //};

            string str;
            while (true)
            {
                Console.Write(" Enter something : ");
                str = Console.ReadLine();
                if (str.Equals("exit"))
                {
                    break;
                }
                else
                {
                    myEvt.MyVal = str;
                }
            }
#endif
            #endregion

            #endregion

            /*--------------------------------------------------------------------------------------------------------*/

            #region ALL TYPE OF  DELEGATE DEMOS

            #region Delegates Discription
            /// ///////////////////////////////////////////////////////////////
            //             Delegate Discription
            /// //////////////////////////////////////////////////////////////
            ///  Delegate is a type to encapsulate a method.
            ///  It's used to represent a method by a variable.
            ///
            ///  Types of delegate
            ///  Uni cast.
            ///  Multi cast.
            ///
            ///  Action delegate is used to represent a method with
            ///  Void return type.
            ///
            ///  Func delegate is used to represent methods with at least
            ///  One return type and will return something.
            ///         Also
            ///  Delegates are like callback functions  in other languages
            ///
            /// ///////////////////////////////////////////////////////////////
            #endregion

            #region  ACTION AND FUNC DELEGATE DEMO
#if ACTION_n_FUNC_DELEGATE_DEMO
            //  Action delegate is used to represent a method with
            //  Void return type.
            // in simple action delegate doesn't return a value
            Action               a1 = () => Console.WriteLine("It is a test");
            Action <string>      a2 = (msg) => Console.WriteLine(msg);
            Action <string, int> a3 = (msg, counter) => {
                for (int i = 1; i <= counter; i++)
                {
                    Console.WriteLine(msg);
                }
            };

            // call to the functions
            a1();
            a2("the Message goes here");
            a3("just a random repeting message", 5);

            //  Func delegate is used to represent methods with at least
            //  One return type and will return something
            // in simple func delegate will always return at least one value
            Func <int>              f1 = () => 10;                       // this returns an int
            Func <string, int>      f2 = str => str.Length;              // this will return a string and an int
            Func <string, int, int> f3 = (str, num) => str.Length * num; // this just is a f*****g dumb example
#endif
            #endregion

            #region Generic Delegate Demo
#if GENERIC_DELEGATE_DEMO
            double dnumA = 6;
            double dnumB = 3;
            GenericDelegate <string> ConcatDelegate = new GenericDelegate <string>(CombinStrings);
            //ConcatDelegate <double> avg = AverageOfTwo; // this can't be done  because the types don't match

            Console.WriteLine("Concatination of two strings {0}+{1} = {2}", dnumA, dnumB, ConcatDelegate(dnumA.ToString(), dnumB.ToString()));

            GenericDelegate <double> mathsDelegate = AverageOfTwo;
            Console.WriteLine("Average of two numbers {0} + {1} / 2 = {2}", dnumA, dnumB, mathsDelegate(dnumA, dnumB));
            mathsDelegate = ModOFTwo;
            Console.WriteLine("Mod of two numbers {0} % {1} = {2}", dnumA, dnumB, mathsDelegate(dnumA, dnumB));

            // this is called an annonymous delegate
            GenericDelegate <string> myDelegate = delegate(string val, string val2){ return(val + val2); };

            // variation in lambda is
            GenericDelegate <string> myDeleWLambda         = (thing1, thing2) => { return(thing1 + thing2); };
            GenericDelegate <int>    integerLambdaDelegate = (thing1, thing2) => { return(thing1 * thing2); };
#endif
            #endregion

            #region  Simple Delegate Demo
#if SIMPLE_DELEGATE_DEMO
            int numA = 6;
            int numB = 2;

            SimpleDelegate simpDele = new SimpleDelegate(Add);
            Console.WriteLine("{0} + {1} = {2} ", numA, numB, simpDele(numA, numB));

            simpDele += Mul; // changing function on the fly
            Console.WriteLine("{0} x {1} = {2} ", numA, numB, simpDele(numA, numB));

            simpDele += Div; // again changing value of delegate to devision function
            Console.WriteLine("{0} / {1} = {2} ", numA, numB, simpDele(numA, numB));

            simpDele += Sub;// and again changing it
            Console.WriteLine("{0} - {1} = {2} ", numA, numB, simpDele(numA, numB));

            // a dictionary of FUnctions
            Dictionary <string, SimpleDelegate> simpleDelegateFuncs = new Dictionary <string, SimpleDelegate>();
            simpleDelegateFuncs.Add("Addition       : ", Add);
            simpleDelegateFuncs.Add("Multiplication : ", Mul);
            simpleDelegateFuncs.Add("Subtraction    : ", Sub);
            simpleDelegateFuncs.Add("Division       : ", Div);

            // using usual method
            //foreach (KeyValuePair<string, SimpleDelegate> pair in simpleDelegateFuncs) {
            //    Console.WriteLine("{0} of {1} and {2} = {3}", pair.Key, numA, numB, pair.Value(numA, numB);
            //}

            // using Lambda expression
            simpleDelegateFuncs.ToList().ForEach((pair) => Console.WriteLine("{0} of {1} and {2} = {3}", pair.Key, numA, numB, pair.Value(numA, numB)));

            // there is no differen in output just saves your time this way
#endif
            #endregion

            #region Local Delegate Demo
#if LOCAL_DELEGATE_DEMO
            numberFunctionDelegate nf;

            nf = Square;
            Console.WriteLine(" Square of 5 is {0} ", nf(5));
            nf = Cube;
            Console.WriteLine(" Cube of 5 is {0} ", nf(5));

            List <numberFunctionDelegate> functions = new List <numberFunctionDelegate>();
            functions.Add(Square);
            functions.Add(Cube);

            //// simple way
            foreach (var function in functions)
            {
                Console.WriteLine(function(5));
            }

            // dictionary way with lambda expression
            Dictionary <string, numberFunctionDelegate> functionDictionary = new Dictionary <string, numberFunctionDelegate>();
            functionDictionary.Add("Square", Square);
            functionDictionary.Add("Cube", Cube);

            //functionDictionary.ToList<KeyValuePair<string, numberFunction>>().
            //    ForEach((pair) => Console.WriteLine(pair.Key+" : "+ pair.Value(5)));

            functionDictionary.ToList().ForEach((pair) => Console.WriteLine(pair.Key + " : " + pair.Value(5)));

            //foreach (KeyValuePair<string, numberFunctionDelegate> item in functionDictionary) {

            //}

            //foreach (var key in functionDictionary.Keys) {
            //    foreach (var val in functionDictionary.Values) {

            //    }
            //}
#endif
            #endregion

            #endregion
            Console.ReadKey();
        }
Example #16
0
 public static ArgumentNullException ExpectArgumentNullException(GenericDelegate del, string paramName)
 {
     return(ExpectArgumentNullExceptionStandard(del, paramName));
 }
Example #17
0
 public GenericCommand(GenericDelegate d)
 {
     _delegate = d;
 }
    void Update()
    {
        if(mState == States.NONE)
            return;

        mAlpha.Update();
        switch(mState) {
        case States.IN:
            if(mAlpha.Ended) {
                if(SetScene(mNextScene)) {
                    mState = States.OUT;
                    mAlpha.Reset(0, Globals.ANIMATIONDURATION);
                }
            }
            break;
        case States.OUT:
            /*if(mSceneCurrent == "Player" || mSceneCurrent == "Export")
                mAlpha.End();*/

            if(mAlpha.Ended) {
                mState = States.NONE;

                if(OnFinished != null) {
                    OnFinished();
                    OnFinished = null;
                }
            }
            break;
        }
        mCurrentColor.a = mAlpha.Value;
    }