简体   繁体   English

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

[英]How can I build a C# list using data from my Class

I am a junior developer with very little experience using a class.我是一名初级开发人员,使用 class 的经验很少。 I have create a class which looks like this.我创建了一个看起来像这样的 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; }


}

} I use this code to populate the Class via the click of an asp button我使用此代码通过单击 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;

I assume everything is working and I am populating the class with records.我假设一切正常,我正在用记录填充 class。 I now want to build a list so can pull the data from the Class.我现在想建立一个列表,以便可以从 Class 中提取数据。 And this is now where I am stuck I instantiate the list using这就是我现在被卡住的地方我使用实例化列表

List<Employee> Emp = new List<Employee>();
 foreach (var item in Employee)
        {

        }

But from there I cannot figure out how to add records from the class.但从那里我无法弄清楚如何从 class 添加记录。

I tried to build a foreach loop but I get a error "Employee is a type, which is not valid in the given context" I can find several example where the values I am trying to get from the class are hard coded but nothing where the data comes from the class.我尝试构建一个 foreach 循环,但出现错误“员工是一种类型,在给定的上下文中无效”我可以找到几个示例,其中我试图从 class 获取的值是硬编码的,但没有数据来自 class。

The goal is to have aa group of textboxes with data.目标是拥有一组带有数据的文本框。 When the users provides the data in those boxes they would click a button that would take the values in the textboxes and store it in the class.当用户在这些框中提供数据时,他们将单击一个按钮,该按钮将获取文本框中的值并将其存储在 class 中。 The code behind would then clear the textboxes of their values and the user can now enter another batch of information that would get added to the class.然后后面的代码将清除其值的文本框,用户现在可以输入另一批将添加到 class 的信息。

When the user was done entering the records I would want to loop thru the class and for each record in the class perform some simple mathematical calculations.当用户完成输入记录时,我想循环通过 class 并为 class 中的每条记录执行一些简单的数学计算。 So it kind of works like a calculator.所以它有点像计算器。

On this way you can add values to list.通过这种方式,您可以将值添加到列表中。

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);
    }
 }

edit编辑

Okay, your new edit makes things a little clearer, but you still don't say what you wish to do with your data once it's been added to your Employee class.好的,您的新编辑使事情变得更加清晰,但是您仍然没有说出一旦将数据添加到您的员工 class 后您希望如何处理数据。

Assuming that you're using ASP.NET, your code is probably structured in a pattern known as MVC, where M(for Model) is your Employee class, your ASP page is the V (for View) and you'll have another class/file that is the C (Controller).假设您使用的是 ASP.NET,您的代码可能采用称为 MVC 的模式构造,其中 M(代表模型)是您的员工 class,您的 ASP 页面是 V(代表视图),您将拥有另一个类/文件即 C(控制器)。

Your UI page is used to populate an instance of Employee, as you've said, this is then passed through the controller to be stored, typically in a database of some sort.正如您所说,您的 UI 页面用于填充 Employee 的实例,然后通过 controller 进行存储,通常存储在某种数据库中。

The controller should provide either an empty instance of Employee to the View when it opens. controller 应该在视图打开时向视图提供一个空的 Employee 实例。

The "submit" button should trigger/call a Task in the controller to store the Employee instance. “提交”按钮应触发/调用 controller 中的Task以存储 Employee 实例。 This may be on a list locally in the Controller or by direct call to the database, depending on your caching protocol.这可能在 Controller 中的本地列表中,或者通过直接调用数据库,具体取决于您的缓存协议。

Once stored the Controller should provide the View with a new, empty Employee instance.存储后 Controller 应该为 View 提供一个新的空 Employee 实例。

Does this seem to make sense and is this relevant to your situation?这似乎有意义吗?这与您的情况有关吗?

original answer below下面的原始答案

Your question is a little unclear.你的问题有点不清楚。

If you are gathering your data to populate your Employee instance from an ASP page and then looking at storing that data onto a remote server then how that is done will be dependent on your remote API.如果您正在收集数据以从 ASP 页面填充您的 Employee 实例,然后考虑将该数据存储到远程服务器上,那么如何完成将取决于您的远程 API。

You could get all of the data from any class properties Something like this?您可以从任何 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: Output:

Employee:EmpID=1234;Hours=46;Minutes=32;Pages=5;Certs=none;HourlyRate=12.45;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何存储我的清单 <List<Class> &gt;在WPF中使用C#转换为文本文件? - How can i store my List<List<Class>> to a text file using C# in WPF? 如何使用ActiveXObject从JavaScript创建C#Com类 - How Can I Create My C# Com Class From JavaScript Using ActiveXObject 如何从单独的 class 中提取字符串,同时将 class 保留在我的主列表中(C#,Unity) - How can I pull a string from a separate class while keeping the class in a list in my main (C#, Unity) 如何建立我的c#类库和参考? - How can I build my c# class library along with the reference? 我如何使用C#将表中的数据添加到WPF中的列表视图中 - How i can add data from table into list view in wpf using C# 如何使用C#将网站中的数据检索到Winforms - How can I retrieve data from a website to my winforms using C# 如何将列表从我的第一个表单传递到第二个表单,向其中添加数据,然后在我的第一个表单上显示它? C# - How can i pass a list from my first form to a second form, add data to it and then show it on my first form? C# 如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C# - How can I use a class and constructor in a another class, method and store them to my list? C# 如何添加将类引用数据返回到我的C#类中的函数? - How can I add a function that returns class reference data into my c# class? C#:如何在Unity中使用数组而不是列表重写用于管理游戏数据序列化的代码? - C#: How can I rewrite my code for managing Game Data Serialization using an Array instead of a List in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM