0

I have a simple windows form application. Basically, I want to save the user's input of their ID number and name. I have a Person class:

class Person
{
    private string nric;
    private string name;

    public Person(string nric, string name)
    {
        this.nric = nric;
        this.name = name;
    }

And the .cs file:

    private void save_Click(object sender, EventArgs e)
    {
        List<Person> pList = new List<Person>();
        Person p1 = new Person(nric.Text, name.Text);
        pList.Add(p1);

        using (TextWriter tw = new StreamWriter("PersonFile.txt"))
        {
            foreach (Person p in pList)
            {
                tw.WriteLine(p);
            }
            tw.Close();
        }
    }

When I tried to run the codes, this is the output shown in the PersonFile.txt:

   Testing.Person

How do I make it such that the ID number and name would be displayed in the text file?

F. Parker
  • 33
  • 1
  • 2
  • 6

5 Answers5

8

Add "ToString" method.

class Person
{
    private string nric;
    private string name;

    public Person(string nric, string name)
    {
        this.nric = nric;
        this.name = name;
    }

    public override string ToString()
    {
       return $"{nric} {name}";
    }
}
Miguel Costa
  • 149
  • 8
  • 1
    I'd make use of the C# string formatting to make it look nicer `$"{nric} {name}"` – Ralt Jun 22 '17 at 09:35
  • @Ralt, I agree with you;) Thanks – Miguel Costa Jun 22 '17 at 09:38
  • 1
    `ToString()` is a very bad option. It is useful only for debugging or displaying something on UI. https://stackoverflow.com/a/563859/6552578 – apocalypse Jun 22 '17 at 10:01
  • @apocalypse I'm not agree with you in this situation. I answered to explain the reason for appearing "Testing.Person" – Miguel Costa Jun 22 '17 at 10:15
  • @MiguelCosta: but in this answer https://stackoverflow.com/a/44695674/6552578 you made a comment which says your solution (ToString) is better. IMO your solution is much worse. – apocalypse Jun 22 '17 at 12:45
1

The line

 tw.WriteLine(p);

simply calls p.ToString() and writes the returning string into your file. The default implementation of ToString() returns the name of the object's type.

One way to solve your issue is to overwrite ToString():

class Person
{
    private string nric;
    private string name;

    public Person(string nric, string name)
    {
        this.nric = nric;
        this.name = name;
    }

    public override string ToString()
    {
        return $"{nric} {name}";
    }
}

If you actually want to save the list in a way you can later load it again, you may want to read about serialization. There are easy ways to store and load objects in different formats (like xml, binary etc).

René Vogt
  • 43,056
  • 14
  • 77
  • 99
0

override the toString() for the class

class Person
{
    private string nric;
    private string name;

    public override string ToString()
    {
       return name + " " + nric;
    }
}

then (no need to create StreamWriter):

File.WriteAllLines("PersonFile.txt", pList);
igorc
  • 2,024
  • 2
  • 17
  • 29
0

You just print the Type of the Object.

Try this:

Tw.WriteLine($"Id: {p.nric}, Name: {p.name}");
ASh
  • 34,632
  • 9
  • 60
  • 82
MyHomieJR
  • 75
  • 1
  • 7
  • `nric` and `name` are `private`, so this won't compile. – René Vogt Jun 22 '17 at 09:36
  • 1
    Just add getters and setters and you are fine – MyHomieJR Jun 22 '17 at 09:38
  • @RenéVogt, formatting output in WriteLine method is more sensible solution than adding `ToString` override for that purpose alone (and serialization rules them both). And **very likely** `Person` class has properties Nric and Name, because it becomes black box otherwise. – ASh Jun 22 '17 at 18:09
0

You can add function to Person class

public string getPerson()
{
    return this.nric + " "+ thisname;
}

and change

tw.WriteLine(p);

to

call tw.WriteLine(p.getPerson());
reboris
  • 9
  • 1
  • 1
    In my opinion, adding ToString is the best way. – Miguel Costa Jun 22 '17 at 09:43
  • c# naming convention is : `public string GetPerson()`. `Person` class as data structure shouldn't be responsible for formatted output, existance of GetPerson method in Person class is very questionable – ASh Jun 22 '17 at 18:16