繁体   English   中英

如何使用来自我的 Class 的数据构建 C# 列表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM