![](/img/trans.png)
[英]How can i store my List<List<Class>> to a text file using C# in WPF?
[英]How can I build a C# list using data from my Class
我是一名初級開發人員,使用 class 的經驗很少。 我創建了一個看起來像這樣的 class。
public class Employee
{
public string EmpID { get; set; }
public string Hours { get; set; }
public string Minutes { get; set; }
public string Pages { get; set; }
public string Certs { get; set; }
public double HourlyRate { get; set; }
}
我使用此代碼通過單擊 asp 按鈕填充 Class
Employee emp1 = new Employee();
emp1.EmpID = ddlSpecialist.SelectedValue;
emp1.Hours = txtDetailsHours.Text;
emp1.Minutes = txtDetailsMinutes.Text;
emp1.Pages = TxtDetailsPages.Text;
emp1.Certs = txtDetailsCertified.Text;
emp1.HourlyRate = HR;
我假設一切正常,我正在用記錄填充 class。 我現在想建立一個列表,以便可以從 Class 中提取數據。 這就是我現在被卡住的地方我使用實例化列表
List<Employee> Emp = new List<Employee>();
foreach (var item in Employee)
{
}
但從那里我無法弄清楚如何從 class 添加記錄。
我嘗試構建一個 foreach 循環,但出現錯誤“員工是一種類型,在給定的上下文中無效”我可以找到幾個示例,其中我試圖從 class 獲取的值是硬編碼的,但沒有數據來自 class。
目標是擁有一組帶有數據的文本框。 當用戶在這些框中提供數據時,他們將單擊一個按鈕,該按鈕將獲取文本框中的值並將其存儲在 class 中。 然后后面的代碼將清除其值的文本框,用戶現在可以輸入另一批將添加到 class 的信息。
當用戶完成輸入記錄時,我想循環通過 class 並為 class 中的每條記錄執行一些簡單的數學計算。 所以它有點像計算器。
通過這種方式,您可以將值添加到列表中。
public class Employee{
public string EmpID { get; set; }
public string Hours { get; set; }
public string Minutes { get; set; }
public string Pages { get; set; }
public string Certs { get; set; }
public double HourlyRate { get; set; }
};
public static void Main()
{
List<Employee> employees = new List<Employee>()
{
new Employee() {EmpID = 1},
new Employee() {EmpID = 2}
};
//or this way
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() {EmpID = 1});
employees.Add(new Employee() {EmpID = 2});
foreach(var employee in employees)
{
Console.WriteLine(employee.EmpID);
}
}
編輯
好的,您的新編輯使事情變得更加清晰,但是您仍然沒有說出一旦將數據添加到您的員工 class 后您希望如何處理數據。
假設您使用的是 ASP.NET,您的代碼可能采用稱為 MVC 的模式構造,其中 M(代表模型)是您的員工 class,您的 ASP 頁面是 V(代表視圖),您將擁有另一個類/文件即 C(控制器)。
正如您所說,您的 UI 頁面用於填充 Employee 的實例,然后通過 controller 進行存儲,通常存儲在某種數據庫中。
controller 應該在視圖打開時向視圖提供一個空的 Employee 實例。
“提交”按鈕應觸發/調用 controller 中的Task
以存儲 Employee 實例。 這可能在 Controller 中的本地列表中,或者通過直接調用數據庫,具體取決於您的緩存協議。
存儲后 Controller 應該為 View 提供一個新的空 Employee 實例。
這似乎有意義嗎?這與您的情況有關嗎?
下面的原始答案
你的問題有點不清楚。
如果您正在收集數據以從 ASP 頁面填充您的 Employee 實例,然后考慮將該數據存儲到遠程服務器上,那么如何完成將取決於您的遠程 API。
您可以從任何 class 屬性中獲取所有數據 像這樣?
using System.IO;
using System;
using System.Text;
using System.Reflection;
using System.Linq;
class Program
{
static void Main()
{
Employee emp1 = new Employee();
emp1.EmpID = "1234";
emp1.Hours = "46";
emp1.Minutes = "32";
emp1.Pages = "5";
emp1.Certs = "none";
emp1.HourlyRate = 12.45;
StringBuilder sb = new StringBuilder();
sb.Append($"{emp1.GetType().Name}:");
var props = emp1.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead);
foreach (var p in props)
{
sb.Append($"{p.Name}=");
sb.Append($"{p.GetValue(emp1).ToString()};");
}
Console.WriteLine(sb.ToString());
}
}
Output:
Employee:EmpID=1234;Hours=46;Minutes=32;Pages=5;Certs=none;HourlyRate=12.45;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.