Как создать объект делегата и вызвать его? - C#
Формулировка задачи:
Добрый день! Написал код, нужно: создать объект делегата и вызвать его
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication66 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } delegate int A(int a); delegate string B(string b); // создал делегаты public static string nat(string n) { return n; } public static int age(int a) { return a; } public static string s(string sud) { return sud; } public static string obraz(string obr) { return obr; } public static int staj(int s) { return s; // Организуем методы } public void meth() { int ball = 0; B op1 = new B(nat); // Сконструируем делегат string names = op1(textBox1.Text); if (textBox1.Text == "русский") // национальность { ball += 1; } else { ball = 0; } string sud = op1(textBox2.Text); if (textBox2.Text == "да") { ball = 0; } else { ball += 1; } op1 = new B(obraz); // Сконструируем делегат string ob = op1(textBox5.Text); if (textBox5.Text == "высшее") { ball += 1; } if (textBox5.Text == "среднее") { ball += 1; } if (textBox5.Text == "нет") { ball += 0; } A op2 = new A(age); // Сконструируем делегат int op = op2(Convert.ToInt16(textBox3.Text)); if (Convert.ToInt16(textBox3.Text) < 25) // Возраст { ball = 0; } else { ball += 1; } A op4 = new A(staj); // Сконструируем делегат op2(Convert.ToInt16(textBox6.Text)); // Стаж if (Convert.ToInt16(textBox6.Text) < 4) { ball = 0; } else { ball += 1; } if (ball >3 ) { textBox4.Text = "Вы приняты на работу количество ваших баллов "; } else { textBox4.Text = "Вы не приняты на работу количество ваших баллов "; } } private void button1_Click(object sender, EventArgs e) { meth(); } } }
Решение задачи: «Как создать объект делегата и вызвать его?»
textual
Листинг программы
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateExample { // Declaration public delegate void myDelegate(string s); class myClass { public void myMethod1(string s) { Console.WriteLine(s + " from instance method1\n"); } public static void myMethod2(string s) { Console.WriteLine(s + " from static method2\n"); } } class Program { static void Main(string[] args) { //Example illustrating different ways to work with //delegate objects //(1) Working with instance methods. //Instantiate a null delegate object called delObj1. myDelegate delObj1 = null; //Delegate Instantiation //Instantiate an object called myObj from myClass //class. myClass myObj = new myClass(); //Assign instance method myMethod1 to delegate //object delObj1. delObj1 = myObj.myMethod1; //Use the delegate object delObj1 to pass some data //to myMethod1. delObj1("Hello"); //Invocation //(2) Working with static methods. //Instantiate a null delegate object called delObj2. myDelegate delObj2 = null; //Delegate Instantiation //Assign static method myMethod2 to delegate object //delObj2. Note that because myMethod2 is a static //method, there is no need to first instantiate an //object from myClass in order to then assign //myMethod2 to delegage object delObj2. delObj2 = myClass.myMethod2; //Use the delegate object delObj2 to pass some //data to myMethod2 delObj2("Greetings"); //Invocation //(3) Working with instance and static methods to //build an invocation list //Create two delegate objects and directly assign //them to their respective instance and static methods. //Delegate instantiation myDelegate delObj3 = new myDelegate(myObj.myMethod1); //and invocation delObj3("Building invocation list, element 1"); //Delegate instantiation myDelegate delObj4 = new myDelegate(myClass.myMethod2); //and invocation delObj4("Building invocation list, element 2"); //Build an invocation list from these two //delegate objects myDelegate delObjTotal = delObj3 + delObj4; //Process the invocation list with some data delObjTotal("Processing entire invocation list."); //Remove a method from invocation list delObjTotal -= delObj4; //Process remaining methods in invocation list delObjTotal("Last message is"); //Alternate way to build invocation list myDelegate delObj5 = null; delObj5 += new myDelegate(myObj.myMethod1); delObj5 += new myDelegate(myClass.myMethod2); //Process total invocation list with some data delObj5("bye..."); //Remove a method from invocation list delObj5 -= myClass.myMethod2; //Process remaining methods in invocation list delObj5("Final message is"); //Pause until user hits enter key Console.ReadLine(); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д