C#委托问题

时间:2011-03-08 16:08:00

标签: c# delegates

我从C#Windows应用程序中收到以下错误:

  

错误1'CreateLabelInPanel'没有重载匹配委托'WorksOrderStore.ProcessDbConnDetailsDelegate'H:\ c \ WorksOrderFactory \ WorksOrderFactory \ WorksOrderClient.cs 43 39 WorksOrderFactory

我有3个.cs文件,基本上是:

  1. 打开一个窗口
  2. 有用户连接数据库的选项
  3. 选择该选项后,系统将关闭并连接到数据库,并加载一些数据(暂时只测试数据)
  4. 然后使用委托,系统应该做soemthing,测试将创建一个标签。但是我还没有对这部分进行编码。
  5. 但是在我将此错误排序之前,我无法构建。

    这三个人被称为:

    1. WorksOrderClient.cs(主要是)
    2. WorksOrderStore.cs
    3. LoginBox.cs
    4. 以下是每个文件的代码:

      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)
              {
      
              }
      
      
          }
      }
      

      任何想法??

      干杯,

3 个答案:

答案 0 :(得分:1)

在将委托的定义与您尝试使用的方法的定义进行比较时,有一件事情会跳出来:

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

CreateLabelInPanel可能不应该声明为静态。

答案 1 :(得分:1)

编译器只是说你提供给委托的方法与委托所期望的签名不匹配。

所以,在你的情况下。

//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)..

如果没有,编译器会抱怨。

另外,你有两个名为“DbConnDetails”的课程吗? 我最好的猜测是你指的是“DbConnDetails”,它位于与委托人所期望的名称空间不同的名称空间中。

答案 2 :(得分:0)

感谢@amby和@massif以及我可能错过的其他任何人。

这是学校男生的错误。

事实证明我有另一个文件(我认为我已删除),它还包含一个名为DbConnDetails的类和构造函数。我在解决方案中搜索了DbConnDetails。

为了安全起见,我还重命名了类/构造函数和文件名。

然后我宣布

static void CreateLabelInPanel(DbConnDetails dbcd)

这意味着应用程序现在再次编译/构建。

再次感谢大家。