Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 4th, 2005, 01:49 PM
Registered User
 
Join Date: Aug 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delegates in .NET

i am just trying to understand delegates in .Net, I am using Professional C# wrox book for this example
I created this .dll and i refrenced in DelegateTest project but i am running into compilation error, Do you see where i gone wrong?
Thanks in advance for your help.


//************************************************** *****************
//****** This is the dll that i am using as Library ***************

using System;

namespace DelegateLib
{

    /// <summary>
    /// Summary description for Class1.
    /// </summary>

    public class BubbleSorter
    {
        public delegate bool CompareOp(object lhs, object rhs);

        static public void Sort(object [] sortArray, CompareOp gtMethod)
        {
            for (int i=0 ; i<sortArray.Length ; i++)
            {
                for (int j=i+1 ; j<sortArray.Length ; j++)
                {

                    if (gtMethod(sortArray[j], sortArray[i]))
                    {
                        object temp = sortArray[i];
                        sortArray[i] = sortArray[j];
                        sortArray[j] = temp;
                    }
                }
            }
        }
    }

}



//************************************************** ******************


//************************************************** ******************
//********** This is the client i am using for testing *************

using System;
using DelegateLib;

namespace DelegateTest
{

    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class DelegateTesting
    {
        delegate bool CompareOpTest(object lhs, object rhs);

        static void Main()
        {
            Employee [] employees =
            {
                new Employee("Karli Watson", 20000),
                new Employee("Bill Gates", 10000),
                new Employee("Simon Robinson", 25000),
                new Employee("Mortimer", (decimal)1000000.38),
                new Employee("Arabel Jones", 23000),
                new Employee("Avon from 'Blake's 7'", 50000)};
            CompareOpTest employeeCompareOp = new CompareOpTest(Employee.RhsIsGreater);
            BubbleSorter.Sort(employees, employeeCompareOp);

            for (int i=0 ; i<employees.Length ; i++)
                Console.WriteLine(employees[i].ToString());
            Console.ReadLine();
        }
    }

    class Employee // : object
    {
        private string name;
        private decimal salary;

        public Employee(string name, decimal salary)
        {
            this.name = name;
            this.salary = salary;
        }

        public override string ToString()
        {
            return string.Format(name + ", {0:C}", salary);
        }

        public static bool RhsIsGreater(object lhs, object rhs)
        {
            Employee empLhs = (Employee) lhs;
            Employee empRhs = (Employee) rhs;
            return (empRhs.salary > empLhs.salary) ? true : false;
        }
    }
}




//************************************************** ******************

Reply With Quote
  #2 (permalink)  
Old August 6th, 2005, 04:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

In the DelegateTest you dont need to declare the delegate again.
Also the name of the delegate is CompareOp and not CompareOpTest.

Please download the latest code for Professional C# 3rd edition from.

http://www.wrox.com/WileyCDA/WroxTit...load_code.html

Regards
Ankur Verma
Reply With Quote
  #3 (permalink)  
Old August 8th, 2005, 02:31 PM
Registered User
 
Join Date: Aug 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much it worked this time.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with delegates mike_remember ASP.NET 1.0 and 1.1 Professional 2 November 7th, 2006 12:33 AM
Delegates pramos.21d C# 1 April 11th, 2006 03:43 AM
Help with Delegates mike_remember ASP.NET 1.0 and 1.1 Basics 4 October 4th, 2005 07:32 AM
Delegates Ibn_Aziz C# 0 February 3rd, 2004 05:55 AM
VB.Net delegates called in C#? CrazyLegsCooper VS.NET 2002/2003 3 November 6th, 2003 05:41 PM



All times are GMT -4. The time now is 08:23 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.