Guest User

Untitled

a guest
Sep 4th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. C# delegates problem
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using WorksOrderStore;
  11. namespace WorksOrderFactory
  12. {
  13. using WorksOrderStore;
  14.  
  15. public partial class WorksOrderClient : Form
  16. {
  17. LoginBox lb = new LoginBox();
  18. private static WorksOrderDB wodb = new WorksOrderDB();
  19.  
  20. private static int num_conns = 0;
  21.  
  22. public WorksOrderClient()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void connectToADBToolStripMenuItem_Click(object sender, EventArgs e)
  28. {
  29. lb.ShowDialog();
  30. lb.Visible = true;
  31. }
  32.  
  33. public static bool createDBConnDetObj(string username, string password, string database)
  34. {
  35. // increase the number of connections
  36. num_conns = num_conns + 1;
  37. // create the connection object
  38. wodb.AddDbConnDetails(username, password, database, num_conns);
  39. // create a new delegate object associated with the static
  40. // method WorksOrderClient.createLabelInPanel
  41. wodb.ProcessDbConnDetails(new ProcessDbConnDetailsDelegate(CreateLabelInPanel));
  42. return true;
  43. }
  44.  
  45. static void CreateLabelInPanel(DbConnDetails dbcd)
  46. {
  47. Console.Write("hellO");
  48. string tmp = (string)dbcd.username;
  49. //Console.Write(tmp);
  50. }
  51.  
  52. private void WorksOrderClient_Load(object sender, EventArgs e)
  53. {
  54.  
  55. }
  56.  
  57. }
  58. }
  59.  
  60. using System;
  61. using System.Collections.Generic;
  62. using System.Linq;
  63. using System.Text;
  64. using WorksOrderFactory;
  65.  
  66. namespace WorksOrderStore
  67. {
  68. using System.Collections;
  69.  
  70. // Describes a book in the book list:
  71. public struct WorksOrder
  72. {
  73. public string contractor_code { get; set; } // contractor ID
  74. public string email_address { get; set; } // contractors email address
  75. public string date_issued { get; set; } // date the works order was issued
  76. public string wo_ref { get; set; } // works order ref
  77. public string status { get; set; } // status ... not used
  78. public job_status js { get; set; } // status of this worksorder within this system
  79.  
  80. public WorksOrder(string contractor_code, string email_address, string date_issued, string wo_ref) : this()
  81. {
  82. this.contractor_code = contractor_code;
  83. this.email_address = email_address;
  84. this.date_issued = date_issued;
  85. this.wo_ref = wo_ref;
  86. this.js = job_status.Pending;
  87. }
  88. }
  89.  
  90. // Declare a delegate type for processing a WorksOrder:
  91. //public delegate void ProcessWorksOrderDelegate(WorksOrder worksorder);
  92.  
  93. // Maintains a worksorder database.
  94. public class WorksOrderDB
  95. {
  96. // List of all worksorders in the database:
  97. ArrayList list = new ArrayList();
  98.  
  99. // Add a worksorder to the database:
  100. public void AddWorksOrder(string contractor_code, string email_address, string date_issued, string wo_ref)
  101. {
  102. list.Add(new WorksOrder(contractor_code, email_address, date_issued, wo_ref));
  103. }
  104.  
  105.  
  106. // Call a passed-in delegate on each pending works order to process it:
  107. /*public void ProcessPendingWorksOrders(ProcessWorksOrderDelegate processWorksOrder)
  108. {
  109. foreach (WorksOrder wo in list)
  110. {
  111. if (wo.js.Equals(job_status.Pending))
  112. // Calling the delegate:
  113. processWorksOrder(wo);
  114. }
  115. }*/
  116.  
  117. // Add a DbConnDetails to the database:
  118. public void AddDbConnDetails(string username, string password, string database, int conn_num)
  119. {
  120. list.Add(new DbConnDetails(username, password, database, conn_num));
  121. }
  122.  
  123. // Call a passed-in delegate on each dbconndet to process it:
  124. public void ProcessDbConnDetails(ProcessDbConnDetailsDelegate processDBConnDetails)
  125. {
  126. foreach (DbConnDetails wo in list)
  127. {
  128. processDBConnDetails(wo);
  129. }
  130. }
  131. }
  132.  
  133. // statuses for worksorders in this system
  134. public enum job_status
  135. {
  136. Pending,
  137. InProgress,
  138. Completed
  139. }
  140.  
  141.  
  142. public struct DbConnDetails
  143. {
  144. public string username { get; set; } // username
  145. public string password { get; set; } // password
  146. public string database { get; set; } // database
  147. public int conn_num { get; set; } // this objects connection number.
  148. public ArrayList woList { get; set; } // list of works orders for this connection
  149.  
  150. // this constructor just sets the db connection details
  151. // the woList array will get created later .. not a lot later but a bit.
  152. public DbConnDetails(string username, string password, string database, int conn_num) : this()
  153. {
  154. this.username = username;
  155. this.password = password;
  156. this.database = database;
  157. this.conn_num = conn_num;
  158. woList = new ArrayList();
  159. }
  160. }
  161.  
  162. // Declare a delegate type for processing a DbConnDetails:
  163. public delegate void ProcessDbConnDetailsDelegate(DbConnDetails dbConnDetails);
  164. }
  165.  
  166. using System;
  167. using System.Collections.Generic;
  168. using System.ComponentModel;
  169. using System.Drawing;
  170. using System.Data;
  171. using System.Linq;
  172. using System.Text;
  173. using System.Windows.Forms;
  174.  
  175. namespace WorksOrderFactory
  176. {
  177. public partial class LoginBox : Form
  178. {
  179. public LoginBox()
  180. {
  181. InitializeComponent();
  182. }
  183.  
  184. private void LoginBox_Load(object sender, EventArgs e)
  185. {
  186. this.Visible = true;
  187. this.Show();
  188. //usernameText.Text = "Username";
  189. //new Font(usernameText.Font, FontStyle.Italic);
  190. }
  191.  
  192. private void cancelBtn_Click(object sender, EventArgs e)
  193. {
  194. this.Close();
  195. }
  196.  
  197. private void loginBtn_Click(object sender, EventArgs e)
  198. {
  199. // set up a connection details object.
  200. bool success = WorksOrderClient.createDBConnDetObj(usernameText.Text, passwordText.Text, databaseText.Text);
  201.  
  202. }
  203.  
  204. private void LoginBox_Load_1(object sender, EventArgs e)
  205. {
  206.  
  207. }
  208.  
  209.  
  210. }
  211. }
  212.  
  213. static void CreateLabelInPanel(DbConnDetails dbcd)
  214. public delegate void ProcessDbConnDetailsDelegate(DbConnDetails dbConnDetails)
  215.  
  216. //To get this line working...
  217. wodb.ProcessDbConnDetails(new ProcessDbConnDetailsDelegate(SomeMethod1));
  218.  
  219. //The method signature should be like this.
  220. static void SomeMethod1(DbConnDetails dbcd)
  221.  
  222. //OR even this -- Instance/Static methods can be supplied to same delegate.
  223. void SomeInstanceMethod(DbConnDetails dbcd)..
  224.  
  225. static void CreateLabelInPanel(DbConnDetails dbcd)
Add Comment
Please, Sign In to add comment