简体   繁体   中英

C# delegates problem

I am getting the following error from my C# Windows Application:

Error 1 No overload for 'CreateLabelInPanel' matches delegate 'WorksOrderStore.ProcessDbConnDetailsDelegate' H:\\c\\WorksOrderFactory\\WorksOrderFactory\\WorksOrderClient.cs 43 39 WorksOrderFactory

I have 3 .cs files that essentially:

  1. Opens a windows
  2. Has an option for the users to connect to a db
  3. When that is selected, the system will go off and connect to the db, and load some data in (just test data for now)
  4. Then using a delegate, the system should do soemthing, which for testing will be to create a label. However I haven't coded this part yet.

But I can't build until I get this error sorted.

The 3 fiels are called:

  1. WorksOrderClient.cs (which is the MAIN)
  2. WorksOrderStore.cs
  3. LoginBox.cs

Here's the code for each file:

WorksOrderClient.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorksOrderStore;
namespace WorksOrderFactory
{
    using WorksOrderStore;

    public partial class WorksOrderClient : Form
    {   
        LoginBox lb = new LoginBox();
        private static WorksOrderDB wodb = new WorksOrderDB();

        private static int num_conns = 0;

        public WorksOrderClient()
        {
            InitializeComponent();
        }

        private void connectToADBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lb.ShowDialog();
            lb.Visible = true;
        }

        public static bool createDBConnDetObj(string username, string password, string database)
        {
            // increase the number of connections
            num_conns = num_conns + 1;
            // create the connection object
            wodb.AddDbConnDetails(username, password, database, num_conns);
            // create a new delegate object associated with the static
            // method WorksOrderClient.createLabelInPanel
            wodb.ProcessDbConnDetails(new ProcessDbConnDetailsDelegate(CreateLabelInPanel));
            return true;
        }

        static void CreateLabelInPanel(DbConnDetails dbcd)
        {
            Console.Write("hellO");
            string tmp = (string)dbcd.username;
            //Console.Write(tmp);
        }

        private void WorksOrderClient_Load(object sender, EventArgs e)
        {

        }

    }
}

WorksOrderStore.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorksOrderFactory;

namespace WorksOrderStore
{
    using System.Collections;

    // Describes a book in the book list:
    public struct WorksOrder
    {
        public string contractor_code { get; set; } // contractor ID
        public string email_address { get; set; }   // contractors email address
        public string date_issued { get; set; }     // date the works order was issued
        public string wo_ref { get; set; }          // works order ref
        public string status { get; set; }          // status ... not used
        public job_status js { get; set; }          // status of this worksorder within this system

        public WorksOrder(string contractor_code, string email_address, string date_issued, string wo_ref) : this()
        {
            this.contractor_code = contractor_code;
            this.email_address = email_address;
            this.date_issued = date_issued;
            this.wo_ref = wo_ref;
            this.js = job_status.Pending;
        }
    }

    // Declare a delegate type for processing a WorksOrder:
    //public delegate void ProcessWorksOrderDelegate(WorksOrder worksorder);

    // Maintains a worksorder database.
    public class WorksOrderDB
    {
        // List of all worksorders in the database:
        ArrayList list = new ArrayList();

        // Add a worksorder to the database:
        public void AddWorksOrder(string contractor_code, string email_address, string date_issued, string wo_ref)
        {
            list.Add(new WorksOrder(contractor_code, email_address, date_issued, wo_ref));
        }


        // Call a passed-in delegate on each pending works order to process it: 
        /*public void ProcessPendingWorksOrders(ProcessWorksOrderDelegate processWorksOrder)
        {
            foreach (WorksOrder wo in list)
            {
                if (wo.js.Equals(job_status.Pending))
                    // Calling the delegate:
                    processWorksOrder(wo);
            }
        }*/

        // Add a DbConnDetails to the database:
        public void AddDbConnDetails(string username, string password, string database, int conn_num)
        {
            list.Add(new DbConnDetails(username, password, database, conn_num));
        }

        // Call a passed-in delegate on each dbconndet to process it: 
        public void ProcessDbConnDetails(ProcessDbConnDetailsDelegate processDBConnDetails)
        {
            foreach (DbConnDetails wo in list)
            {
                processDBConnDetails(wo);
            }
        }
    }

    // statuses for worksorders in this system
    public enum job_status
    {
        Pending,
        InProgress,
        Completed
    }


    public struct DbConnDetails
    {
        public string username { get; set; } // username
        public string password { get; set; } // password
        public string database { get; set; } // database
        public int conn_num { get; set; } // this objects connection number.
        public ArrayList woList { get; set; }  // list of works orders for this connection

        // this constructor just sets the db connection details
        // the woList array will get created later .. not a lot later but a bit.
        public DbConnDetails(string username, string password, string database, int conn_num) : this()
        {
            this.username = username;
            this.password = password;
            this.database = database;
            this.conn_num = conn_num;
            woList = new ArrayList();
        }
    }

    // Declare a delegate type for processing a DbConnDetails:
    public delegate void ProcessDbConnDetailsDelegate(DbConnDetails dbConnDetails);
}

LoginBox.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorksOrderFactory
{
    public partial class LoginBox : Form
    {
        public LoginBox()
        {
            InitializeComponent();
        }

        private void LoginBox_Load(object sender, EventArgs e)
        {
            this.Visible = true;
            this.Show();
            //usernameText.Text = "Username";
            //new Font(usernameText.Font, FontStyle.Italic);
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loginBtn_Click(object sender, EventArgs e)
        {
            // set up a connection details object.
            bool success = WorksOrderClient.createDBConnDetObj(usernameText.Text, passwordText.Text, databaseText.Text);

        }

        private void LoginBox_Load_1(object sender, EventArgs e)
        {

        }


    }
}

Any ideas??

Cheers,

m

The one thing jumps out when comparing the definition of the delegate with the definition of the method you're trying to use:

static void CreateLabelInPanel(DbConnDetails dbcd)
public delegate void ProcessDbConnDetailsDelegate(DbConnDetails dbConnDetails)

CreateLabelInPanel should probably not be declared as static.

The compiler is just saying that the method you are providing to a delegate is not matching the signature expected by the delegate.

So, in your case.

//To get this line working...
 wodb.ProcessDbConnDetails(new ProcessDbConnDetailsDelegate(SomeMethod1));

//The method signature should be like this.
static void SomeMethod1(DbConnDetails dbcd)

//OR even this -- Instance/Static methods can be supplied to same delegate.
void SomeInstanceMethod(DbConnDetails dbcd)..

And if not, the compiler would complain.

Also, by any chance, do you have two classes with the name "DbConnDetails" ??? My best guess is you are referring to "DbConnDetails" that lies in different namespace than the one expected by the delegate.

thanks @amby and @massif and anyone else I may have missed.

This was a school boy error.

It turns out I had another file (which I thought I'd deleted) that also contained a class and constructor called DbConnDetails. I did a search for DbConnDetails in the solution.

I renamed that class/constructor and the filename as well, to be safe.

I then declared

static void CreateLabelInPanel(DbConnDetails dbcd)

which means that the app now compiles/builds again.

Thanks again to everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM