溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C# 中Employee對象的作用是什么

發(fā)布時間:2021-07-07 17:25:18 來源:億速云 閱讀:341 作者:Leah 欄目:編程語言

這篇文章給大家介紹C# 中Employee對象的作用是什么,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

C# Employee對象

命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函數(shù)接收一個ObjectId參數(shù),它通過一個ref類型的字符串?dāng)?shù)組返回值(包含相應(yīng)的雇員數(shù)據(jù))。調(diào)用它的PrintoutEmployee()函數(shù)只是用來在命令行中輸出這些數(shù)據(jù)。

我們需要一個遍歷并顯示所有雇員數(shù)據(jù)的命令。

  1. public static void ListEmployee(ObjectId employeeId, ref string[] saEmployeeList)  

  2. {  

  3. int nEmployeeDataCount = 0;  

  4. Database db = HostApplicationServices.WorkingDatabase;  

  5. Transaction trans = db.TransactionManager.StartTransaction(); //開始事務(wù)處理。  

  6. try  

  7. {  

  8. Entity ent = (Entity)trans.GetObject(employeeId, OpenMode.ForRead, false); 

  9. //打開當(dāng)前對象!  

  10. if (ent.GetType() == typeof(BlockReference))  

  11. {  

  12. //不是所有的塊索引都有雇員數(shù)據(jù),所以我們要處理錯誤  

  13. bool bHasOurDict = true;  

  14. Xrecord EmployeeXRec = null;  

  15. try{  

  16. BlockReference br = (BlockReference)ent;  

  17. DBDictionary extDict = (DBDictionary)trans.GetObject
    (br.ExtensionDictionary, OpenMode.ForRead, false);  

  18. EmployeeXRec = (Xrecord)trans.GetObject(extDict.GetAt("EmployeeData"), 
    OpenMode.ForRead, false);  

  19. }  

  20. catch  

  21. {  

  22. bHasOurDict = false; //出現(xiàn)了錯誤……字典或擴展記錄不能訪問  

  23. }  

  24. if (bHasOurDict) //如果獲得擴展字典,而又有擴展記錄……  

  25. {  

  26. // 為雇員列表分配內(nèi)存  

  27. saEmployeeList = new String[4];  

  28. //加入雇員的名字  

  29. TypedValue resBuf = EmployeeXRec.Data.AsArray()[0];  

  30. saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), 
    nEmployeeDataCount);  

  31. nEmployeeDataCount += 1;  

  32. //加入雇員的薪水  

  33. resBuf = EmployeeXRec.Data.AsArray()[1];  

  34. saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), 
    nEmployeeDataCount);  

  35. nEmployeeDataCount += 1;  

  36. //加入雇員所在的部門  

  37. resBuf = EmployeeXRec.Data.AsArray()[2];  

  38. string str = (string)resBuf.Value;  

  39. saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), 
    nEmployeeDataCount);  

  40. nEmployeeDataCount += 1;  

  41. //現(xiàn)在,讓我們從公司字典中獲取老板的名字  

  42. //在NOD中找到.  

  43. DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, 
    OpenMode.ForRead, false);  

  44. DBDictionary acmeDict = (DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"), 
    OpenMode.ForRead);  

  45. //注意我們直接使用擴展數(shù)據(jù)...  

  46. DBDictionary salesDict = (DBDictionary)trans.GetObject(acmeDict.GetAt
    ((string)EmployeeXRec.Data.AsArray()[2].Value),OpenMode.ForRead);  

  47. Xrecord salesXRec = (Xrecord)trans.GetObject(salesDict.GetAt("Department Manager"), 
    OpenMode.ForRead);  

  48. //***,把雇員的數(shù)據(jù)輸出到命令行  

  49. resBuf = salesXRec.Data.AsArray()[0];  

  50. saEmployeeList.SetValue(string.Format("{0}\n", resBuf.Value), nEmployeeDataCount);  

  51. nEmployeeDataCount += 1;  

  52. }  

  53. }  

  54. trans.Commit();  

  55. }  

  56. finally  

  57. {  

  58. trans.Dispose();  

  59. }  

  60. }  

  61.  

  62. [CommandMethod("PRINTOUTEMPLOYEE")]  

  63. public static void PrintoutEmployee()  

  64. {  

  65. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;  

  66. //聲明我們將在下面使用的工具...  

  67. Database db = HostApplicationServices.WorkingDatabase;  

  68. Transaction trans = db.TransactionManager.StartTransaction();  

  69. try  

  70. {  

  71. //首先,獲取塊表和模型空間塊表記錄  

  72. BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.
    WorkingDatabase.BlockTableId, OpenMode.ForRead);  

  73. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], 
    OpenMode.ForRead);  

  74. //現(xiàn)在,我們需要把內(nèi)容輸出到命令行。這里可以有一個對象幫助我們:  

  75. //下面的部分,我們將遍歷模型空間:  

  76. foreach (ObjectId id in btr)  

  77. {  

  78. Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打開當(dāng)前對象!  

  79. if (ent is BlockReference)  

  80. {  

  81. string[] saEmployeeList = null;// 這是正確的...定義新的列表。  

  82. ListEmployee(id, ref saEmployeeList);  

  83. if ((saEmployeeList.Length == 4))  

  84. {  

  85. ed.WriteMessage("Employee Name: {0}", saEmployeeList[0]);  

  86. ed.WriteMessage("Employee Salary: {0}", saEmployeeList[1]);  

  87. ed.WriteMessage("Employee Division: {0}", saEmployeeList[2]);  

  88. ed.WriteMessage("Division Manager: {0}", saEmployeeList[3]);  

  89. }  

  90. }  

  91. }  

  92. }  

  93. finally  

  94. {  

  95. }  

關(guān)于C# 中Employee對象的作用是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI