I have 2 problems that I need help with:
The first problem is to show the strings in the list which I have imported from the file. Instead of showing the names, I get the name of the namespace + the name of the class ex. inupp_v9.boy
. I've tried to override the strings ToString
but then the list doesn't show anything at all after that.
The second thing that I need help is that I need to use gender as an argument in the constructor for the Person
class, and that it is appropriate to use an enum for gender. But the problem is that I have no idea for what or how to use it. If anybody can come up with an idea that I can use, and how to use it would be great.
For example I have been thinking to use the gender that when I create a new name it will set the name to either male or female depending on which of the 2 radiobuttons I check. That will then export the name to either the girltxt.file
or boytxt.file
.
But I don't know how to do that so any example from you guys that I can use works for me.
form:
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;
}
Classes:
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; }
}
}