Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C# delegates problem
- 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)
- {
- }
- }
- }
- 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);
- }
- 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)
- {
- }
- }
- }
- static void CreateLabelInPanel(DbConnDetails dbcd)
- public delegate void ProcessDbConnDetailsDelegate(DbConnDetails dbConnDetails)
- //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)..
- static void CreateLabelInPanel(DbConnDetails dbcd)
Add Comment
Please, Sign In to add comment