【问题标题】:I can't assign values to the "List Collection" in my class我无法为班级中的“列表集合”赋值
【发布时间】:2013-11-17 22:27:36
【问题描述】:

这是我的类,它包含两个 List 集合,它们将存储从我的主表单发送过来的输入。截至目前,它在我的课程中没有编译错误,但我不确定它是否正确。它应该做的是接收输入并在调用时显示它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    namespace EmployeeRoster
    {

            public class Employee
            {
                List<string> name = new List<string>();
                List<int> iDnumber = new List<int>();


            public void setEmployeeName( List < string> employeenames, string names)
            {
                employeenames.Add(names);
                employeenames = name;
            }

            public List <string> getEmployeeName()
            {
                return name;
            }

            public void setEmployeeNumber( List < int > employeeId, int numbers)
            {
                employeeId.Add(numbers);
                employeeId = iDnumber;
            }


            public List<int> getEmployeeName()
            {
                return iDnumber;
            }
        }
}

我正在尝试将名称分配给我在上面的班级中创建的列表,但是我正在尝试下面的代码并且收到 2 个错误

  1. // 错误无法将类型“System.Collections.Generic.List”隐式转换为“字符串”

  2. // 方法 'setEmployeeName' 的错误没有重载需要 1 个参数

为了填写我的列表,将参数发送到 Class 的正确方法是什么?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace EmployeeRoster
    {
        public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Employee chart = new Employee();
            List < Employee > names = new List < Employee > ();


            text1.Text = names; // Error Cannot implicitly convert type 'System.Collections.Generic.List<EmployeeRoster.Employee>' to 'string'  

            chart.setEmployeeName(names); // Error No overload for method 'setEmployeeName' takes 1 arguments   


        }
    }
}

【问题讨论】:

    标签: c# list class methods overloading


    【解决方案1】:

    text1.text = Convert.ToString(names);

    您可以像这样即时转换它。

    我认为您不需要将集合传递给班级。该类可以为您实例化字符串并将其添加到集合中。

    【讨论】:

    • 所以我不需要在我的类中调用我的重载方法?哦,转换确实有效,谢谢,但这会将用户输入存储在我的班级创建的列表集合中
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2015-03-09
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多