0

我有 2 个问题需要帮助:

第一个问题是在列表中显示我从文件中导入的字符串。我没有显示名称,而是获得了命名空间的名称 + 类 ex 的名称。inupp_v9.boy. 我试图覆盖字符串ToString,但之后列表根本没有显示任何内容。

我需要帮助的第二件事是我需要在Person类的构造函数中使用性别作为参数,并且对性别使用枚举是合适的。但问题是我不知道什么或如何使用它。如果有人能想出一个我可以使用的想法,以及如何使用它,那就太好了。

例如,我一直在考虑使用性别,当我创建一个新名称时,它将根据我检查的 2 个单选按钮中的哪一个将名称设置为男性或女性。然后将名称导出到girltxt.fileboytxt.file

但是我不知道该怎么做,所以你们可以使用的任何示例都对我有用。

形式:

    private void collectg_Click(object sender, EventArgs e)
    {
        if (_hasBoys)
        {
            return;
        }

        _hasBoys = true;

        List<girl> girls = new List<girl>();

        foreach(var name in System.IO.File.ReadAllLines(@"C:\Path\tofile\girls.txt"))
        {
            girl g = new girl { Name = name };
            girls.Add(g);
        }

        listBox1.DataSource = girls;

    }

    private void collectb_Click(object sender, EventArgs e)
    {
        if (_hasGirls)
        {
            return;
        }

        _hasGirls = true;

        List<boy> boys = new List<boy>();
        foreach (var name in System.IO.File.ReadAllLines(C:\Path\tofile\boys.txt"))
        {
            boy b = new boy { Name = name };
            boys.Add(b);
        }

        listBox1.DataSource = boys;

    }

课程:

namespace Inlämningsuppgift_v9
{
    class Person
    {
        public string Name { get; set; }

    }

    class girl : Person
    {
        public List<girl> girls { get; set; }

        //public override string  ToString()

        //{
        //    return girls.ToString();
        //}

    }
    class boy : Person
    {
        public List<boy> boys { get; set; }
    }
}
4

5 回答 5

2

覆盖ToString方法以查看名称值而不是类名:

class Person
{
    public Gender Gender { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

enum Gender
{
     Male,
     Female
}

如何从派生类设置性别:

class Girl : Person
{        
    public Girl()
    {
        Gender = Gender.Female;
    }
}

顺便说一句,这是您的代码简化:

private void collectg_Click(object sender, EventArgs e)
{
    if (_hasBoys)        
        return;        

    _hasBoys = true;

    listBox1.DataSource = File.ReadAllLines(@"C:\Path\tofile\girls.txt")
                              .Select(line => new Girl { Name = line })
                              .ToList();
}

private void collectb_Click(object sender, EventArgs e)
{
    if (_hasGirls)
        return;

    _hasGirls = true;

    listBox1.DataSource = File.ReadAllLines(@"C:\Path\tofile\boys.txt")
                              .Select(line => new Boy { Name = line })
                              .ToList();
}

另外我建议您将硬编码的文件名移动到配置文件中。实际上,我看不出有任何理由将女孩和男孩分开上课。我认为您可以在这里使用一个 Person 类。像这样:

private IEnumerable<Person> GetPersons(Gender gender)
{
    string fileName = // get file name depending on gender
    return File.ReadAllLines(fileName)
               .Select(line => new Person { Name = line, Gender = gender })
               .ToList();
}

您可以使用这样的创建方法,而不是两个不向基类添加任何数据或行为的类:

class Person
{
    public static Person CreateBoy(string name)
    {
        return new Person { Name = name, Gender = Gender.Male };
    }

    public static Person CreateGirl(string name)
    {
        return new Person { Name = name, Gender = Gender.Female };
    }

    public Gender Gender { get; private set; }
    public string Name { get; private set; }

    public override string ToString()
    {
        return Name;
    }
}

Then creating boys will look like: Person.CreateBoy(name)

于 2013-02-28T16:14:19.910 回答
1

You're asking a bunch of questions here, but I'll do my best to answer them. If the classes boy and girl are just supposed to have a name attribute which is shown when you call ToString(), I'd do it more like:

class girl : Person
{
    public override string  ToString()

    {
        return Name.ToString(); //ToString is redundant though.
    }

}

I'm not sure why you're putting a List<girl> in the class though, so I've removed it.

You can create an Enum very simply:

enum Gender{
   Male,
   Female
}

and then alter your class to accept a new parameter.

于 2013-02-28T16:16:48.947 回答
0

if you read this thread C# List<string> to string with delimiter may be is usefull to your question, also in this website explain ways to call member of a list of string and convert members of list to string http://www.dotnetperls.com/convert-list-string , http://msdn.microsoft.com/en-us/library/system.string.format.aspx Hope that helps.

于 2013-02-28T16:26:19.777 回答
0

Yeah, the ToString() on the person should fix your issue on the list box.

You could also set a specific display member.

listBox1.DisplayMember = "Name";
于 2013-02-28T16:27:59.130 回答
0

To display the name you can override ToString correctly in the Person class;

public override string ToString()
    {
        return Name;
    }

or set the DisplayMemer of the ListBox to "Name" as Kwel stated

listBox1.DisplayMember = "Name";

? Why do you want your boys and girls have lists of boys and girls? ? Why should gender as parameter in the constructor of Person apply a filter set by RadioButtons?

radioButton1.Checked ... Load girls.txt
radioButton2.Checked .. Load boys.txt
于 2013-02-28T16:37:08.233 回答