0

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; }
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
user2043267
  • 71
  • 1
  • 12

5 Answers5

2

Override ToString method to see name value instead of class name:

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

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

enum Gender
{
     Male,
     Female
}

How to set gender from derived class:

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

BTW Here is your code simplified:

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

Also I suggest you to move hard-coded file names to configuration file. And actually I don't see any reason for separate Girl and Boy classes. I think you can use one Person class here. Like this:

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

Instead of two classes which do not add any data or behavior to base class, you can use creation methods like this:

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)

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • @TankorSmash thanks! You answer is also good, but `ToString` is really redundant there – Sergey Berezovskiy Feb 28 '13 at 16:26
  • why i need to separate the classes is because it says so in the task :p, i did the programs function in the form but then was the problem with the classes and i didnt have any idea what to use it for and the classes is the main use in the task :p – user2043267 Feb 28 '13 at 16:28
  • @user2043267 well, I sometimes use dummy classes when working with just for naming reason, like `class UserRepository : Repository`, but I tend not to create classes which do not add data or behavior to base class :) It's better to create Creation Methods on base class, I'll add sample for you – Sergey Berezovskiy Feb 28 '13 at 16:44
  • @user2043267 please do not modify your question. If you will face new problems, just create small, but concrete new questions – Sergey Berezovskiy Feb 28 '13 at 16:52
  • i didnt modify my question, but either way im sorry then, ur answers have been really useful and the last thing i need help with is to show something with my enum, ive been lookin in internet for different outputs with enums but havent found something that i can use with my enums yet. Im thinking to count with a forloop how many enums i have set and the output would be how many males or females there are in txt.file, but i cant make it work – user2043267 Feb 28 '13 at 17:07
  • @user2043267 I don't understand what you mean by counting enum. If you have list of Persons, then `int boysCount = persons.Count(p => p.Gender == Gender.Male)`. Also count of lines in file will tell you same information – Sergey Berezovskiy Feb 28 '13 at 17:17
  • just need something to do with the gender, but the "int boysCount = persons.Count(p => p.Gender == Gender.Male)" is the answer for my question if i can make it work. i switched the persons.count to Name.Count but what i get wrong is the p.gender which says that "'string' does not contain a definition for 'Gender' and no extension method 'Gender' accepting a first argument of type 'string' could be found" – user2043267 Feb 28 '13 at 17:29
  • @user2043267 please create new question for your new problem. I can't understand what you are trying to do - some sample code is needed. – Sergey Berezovskiy Feb 28 '13 at 17:44
  • i have edited my question with what im asking for and code sample at the bottom – user2043267 Feb 28 '13 at 17:58
  • @user2043267 I rolled back your edit, because your question had two parts only - names displaying and passing gender. About your last edit - you are doing something completely wrong. Name looks like string. – Sergey Berezovskiy Feb 28 '13 at 18:00
  • wierd that code sample that i edited is gone now but ill just post it in this comment instead, the code sample is: int boycount = Name.Count(p => p.Gender == Gender.Male); MessageBox.Show("There are " + boycount + "boys in the textfile"); so basicly the boyscount is the number of how many males there are – user2043267 Feb 28 '13 at 18:06
  • @user2043267 it's not gone - I removed it (that what *I rolled back your edit* means). And I already said that you are doing something completely wrong. Sorry, need to go. Ask new question on SO. Somebody will help you. – Sergey Berezovskiy Feb 28 '13 at 18:08
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.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
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.

Community
  • 1
  • 1
  • I agree with the solution of Kwel if the listbox is assigned the Datasource and this contains the data in string format entoces just tell **listBox1.DisplayMember = "Name"** will list all the data in the **Name** field. – Maykel Llanes Garcia Feb 28 '13 at 16:40
  • the first link isnt really useful to me since my task is to import and export the names :) but thanks anyways, the only problem i have now is what to do with my enum (male and female), now that each line in txt.file is either male or female, i need an idea how to show it but i got no clue :) – user2043267 Feb 28 '13 at 16:47
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";
Kwel
  • 97
  • 1
  • 7
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
  • @Kwel didn't saw your comment until posted. –  Feb 28 '13 at 16:38
  • i have some other code in the program which the function is to write a name in a textboxt, then u either check the radiobutton for girls or radiobutton for boys, if the radiobutton for boys is checked to name will export to the boytxt.file and if girl is checked it will export to girltxt.file – user2043267 Feb 28 '13 at 16:43