Help me with a save method.

  • Thread starter Thread starter horatiu_alex
  • Start date Start date
H

horatiu_alex

Guest
I have a basic app that have a list box with some text(Jack Brown is a presidend and is happy). I want to take theese values and save them to a csv row in a text file. I've create a save method is the class Person. Here is the class:

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Job{ get; set; }
public bool IsHappy { get; set; }

public override string ToString()
{
var StatusText = IsHappy ? "happy" : "unhappy";

return $"{FirstName} {LastName} is a {Job} and is {StatusText}";
}
public void Save(StreamWriter writer)
{
string sPath = @"C:\Users\user\Desktop\Document.TXT";
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
SaveFile.Write(FirstName + LastName + Age + IsHappy);
SaveFile.Close();
}
}

The first problem is that I couldn't call this function here:

public void saveListButton_Click(object sender, EventArgs e)
{
Person savefile = new Person();
savefile.Save();
}

The second problem is since the isHappy is a bool, i want to write instead of the status true or false(or 1 or 0). And I don't know how should I do that.

A little mention, this will work at RunTime. The user can enter the name, job, and status(this is a checkbox), and then save them and so on.

Here is the add user script:

private void addUserButton_Click(object sender, EventArgs e)
{
var person = new Person { FirstName = firstNameText.Text, LastName = lastNameText.Text, Job = jobText.Text, IsHappy = isHappyCheckbox.Checked };
_persons.Add(person);
}

Thanks for attention!

Continue reading...
 
Back
Top