Exemple #1
0
        //Run the following code and step through it using breakpoints in order to understand what is happening
        //Bonus Lab
        static void Main(string[] args)
        {
            Comparison comp = new Comparison(); //Calling constructor
            var        a    = new Item();
            var        b    = new Item();

            //using the break point on a and b shows that they are null (absent of values)
            a.Name = "Bob";
            b.Name = "Carly";
            //till the var are assigned a name using the class item public string
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            comp.CompareByName(a.Name, b.Name);
            //Once it gets to this line the breakpoint moves onto the compareTo
            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            comp.CompareByName(a.Name, b.Name);

            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            comp.CompareByName(a.Name, b.Name);

            Console.WriteLine(comp.CompareByLength("Testy", "Hello"));
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var a = new Item();
            var b = new Item();

            a.Name = "Bob";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));

            //Initialise objects and assign name
            Comparison nameA = new Comparison();

            nameA.Name = "Jef";
            Comparison nameB = new Comparison();

            nameB.Name = "Jez";

            //Compare by length, if string is the same return 0 if longer return 1 else return -1
            if (nameA.CompareByLength(nameB.Name) == 0)
            {
                Console.WriteLine("{0} is the same length as {1}", nameA.Name, nameB.Name);
            }
            else if (nameA.CompareByLength(nameB.Name) == 1)
            {
                Console.WriteLine("{0} is longer than {1}", nameA.Name, nameB.Name);
            }
            else
            {
                Console.WriteLine("{0} is shorter than {1}", nameA.Name, nameB.Name);
            }

            //Compare by name, if string are the same return 0
            if (nameA.CompareByName(nameB.Name) == 0)
            {
                Console.WriteLine("{0} is the same as {1}", nameA.Name, nameB.Name);
            }
            else
            {
                Console.WriteLine("{0} is not the same as {1}", nameA.Name, nameB.Name);
            }


            Console.Read();
        }
Exemple #3
0
 static void Main(string[] args) 
 {
     var a = new Item();
     var b = new Item();
     a.Name = "Bob";
     b.Name = "Carly";
     Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
     a.Name = "Carly";
     b.Name = "Carly";
     Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
     a.Name = "Edward";
     b.Name = "Carly";
     Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
 }
        static void Main(string[] args)
        {
            var a = new Item();
            var b = new Item();

            a.Name = "Bob";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));

            /*
             * Compare to is a method which compares two objects of the same type returning an integer
             * which indicates whether the chosen object (instance) is following the sort order
             * of another object
             */

            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
        }
        static void Main(string[] args)
        {
            var a = new Item();
            var b = new Item();

            a.Name = "Bob";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));

            Application app = new Application();

            app.Name = "MY APPLICATION";
            app.initialise();

            //Console.Read();
        }
        static void Main(string[] args)
        {
            var a = new Item();
            var b = new Item();

            a.Name = "Bob";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));

            var string1 = new Comparison();
            var string2 = new Comparison();

            string1.Name = "Yash";
            string2.Name = "Chatim";

            int alphabet = string1.CompareByName(string2);
            int length   = string1.CompareByLength(string2);

            string textAlphabet = alphabet.ToString();
            string textLength   = length.ToString();

            switch (alphabet)
            {
            case -1:
                textAlphabet = "smaller than";
                break;

            case 0:
                textAlphabet = "same as";
                break;

            case 1:
                textAlphabet = "greater than";
                break;
            }

            switch (length)
            {
            case -1:
                textLength = "shorter";
                break;

            case 0:
                textLength = "same";
                break;

            case 1:
                textLength = "longer";
                break;
            }


            Console.WriteLine("{0} is {1} {2} alphabetically", string1.Name, textAlphabet, string2.Name);
            Console.WriteLine("{0} is {1} length compared to {2}", string1.Name, textLength, string2.Name);

            Console.ReadLine();
        }
Exemple #7
0
        static void Main(string[] args)
        {
            var a = new Item();
            var b = new Item();

            a.Name = "Bob";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Carly";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));
            a.Name = "Edward";
            b.Name = "Carly";
            Console.WriteLine("{0} compared to {1} is {2}", a.Name, b.Name, a.CompareTo(b));

            /*Less than zero: This instance precedes obj in the sort order.
             * Zero: This instance occurs in the same position in the sort order as obj.
             * Greater than zero: This instance follows obj in the sort order.
             */

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("============================= COMPARE BY LENGTH ==============================\n");
            ICompareByLength scribe1 = new Comparison();

            Console.WriteLine("Enter a word and press enter:\n");
            var input1 = Console.ReadLine();

            Console.WriteLine("Enter another word and press enter:\n");
            var input2  = Console.ReadLine();
            var output1 = scribe1.CompareByLength(input1, input2);

            switch (output1)
            {
            case 1: Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("{0} is greater than {1} ", input1, input2);
                break;

            case -1: Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0} is smaller than {1}", input1, input2);
                break;

            case 0:
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("{0} is equal to {1}", input1, input2);
                break;
            }
            Console.ForegroundColor = ConsoleColor.DarkMagenta;

            Console.WriteLine("============================= COMPARE BY NAME ============================");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Enter a word and press enter:\n");
            var input3 = Console.ReadLine();

            Console.WriteLine("Enter another word and press enter:\n");
            var            input4  = Console.ReadLine();
            ICompareByName scribe2 = new Comparison();
            var            output2 = scribe2.CompareByName(input3, input4);

            switch (output2)
            {
            case 1:
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("{0} follows {1} in the alphabet.", input3, input4);
                break;

            case -1:
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0} precedes {1} in the alphabet..", input3, input4);
                break;

            case 0:
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("{0} is equal to {1} in the alphabet.", input3, input4);
                break;
            }
            Console.ForegroundColor = ConsoleColor.White;
        }