In this tutorial, I tested a collinear function for three points.
I also recapitulated access to the method and class without using lambda expression with this calculation method.
The example contains two classes. A class is simple for recapitulating knowledge and can be tested by commenting and uncommenting the source code.
The other class calculates the collinearity by the area method.
Here is the commented source code for a better understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | using System; // create delegate Test for default class with arg int i public delegate void Test(int i); // create delegate DelegateColliniar for my test class with each points // P1 (x1, y1) ... public delegate string DelegateColliniar(int x1, int y1, int x2, int y2, int x3, int y3); // main program public class Program { // main method public static void Main () { // set value for each points P1 (x1, y1) ... // I will show the output like this in comment //result : and_result // you can get some warnings if not use these // ... The variable 'x1' is assigned but its value is never used ... int x1 = 1, x2 = 1, x3 = 1, y1 = 1, y2 = 4, y3 = 5; // DELEGATE AREA // STATIC method //example of use delegate with default class method //STEP 1 //Test newTest1 = new Test(DefaultClass.testMethod_static); //newTest1(1); // result : Method test: 1 //STEP 2 // or you can use //Test newTest1 = (int i) => Console.WriteLine(i); //newTest1(2); // result : 2 //STEP 3 // or using one of the definitions of Test newTest1 = , with Invoke //newTest1.Invoke(3); // with Test newTest1 = new Test(DefaultClass.testMethod_static); // result : Method test: 3 // with Test newTest1 = (int i) => Console.WriteLine(i); // result : 3 // NON-STATIC method - Error //main.cs(39,26): error CS0120: An object reference is required for the non-static field, method, or property 'DefaultClass.testMethod_nonstatic(int)' [/home/runner/Classes001/main.csproj] //main.cs(40,1): error CS0103: The name 'newTest1' does not exist in the current context [/home/runner/Classes001/main.csproj] //The build failed. Please fix the build errors and run again. //exit status 1 // See example: //Test newTest2 = new Test(DefaultClass.testMethod_nonstatic); //newTest1(11); // WITHOUT DELEGATE AREA //DefaultClass test = new DefaultClass(); //STEP 1 //test.testMethod_nonstatic(1); // result : Method test: 1 //STEP 2 //test.testMethod_static(2); //result : ... //main.cs(52,1): error CS0176: Member 'DefaultClass.testMethod_static(int)' cannot be accessed with an instance reference; qualify it with a type name instead [/home/runner/Classes001/main.csproj] //The build failed. Please fix the build errors and run again. //exit status 1 // MY EXAMPLE // DELEGATE DelegateColliniar testcollinear = new DelegateColliniar(ThreePoints.collinear); //testcollinear(1, 1, 1, 1, 4, 5); //result : True //testcollinear(x1, y1, x2, y2, x3, y3); // result :True //testcollinear.Invoke(1, 1, 1, 1, 4, 5); // result : warning CS0219: The variable ... is assigned but its value is never used [/home/runner/Classes001/main.csproj] //True testcollinear.Invoke(x1, y1, x2, y2, x3, y3); // result :True // WITHOUT DELEGATE // you will get error if don't remove static word from definition of collinear in ThreePoints // public string collinear(int x1, int y1, int x2, int y2, int x3, int y3) //ThreePoints withoutDelegate = new ThreePoints(); //Console.WriteLine("Is collinear ? " + withoutDelegate.collinear(x1, y1, x2, y2, x3, y3)); } } // default class public class DefaultClass { public static void testMethod_static(int i) { Console.WriteLine("Method test: " + i); } public void testMethod_nonstatic(int i) { Console.WriteLine("Method test: " + i); } } // my test class public class ThreePoints { /* function to check if point collinear or not */ public static string collinear(int x1, int y1, int x2, int y2, int x3, int y3) { /* Calculation the area of triangle if is zero these points are collinear */ int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); String msg; if (a == 0) { //Console.Write("Yes"); msg = "True"; } else { //Console.Write("No"); msg = "False"; } Console.WriteLine(msg); return msg; } } |