Reputation: 2414
I am writing a program where I want to be able to add a Surfer
(a class that requires four strings, FirstName
, LastName
, Dob
, Country
). (I did not use DateTime
as it wasn't a necessity for this project although it is probably better practice to use it.) I then wanted to write the class I just created to a text file, and I have a formatted string for this. I have two issues.
It is not writing the Dob
or the Country
to the text file.
I am not sure how to put each entry on a new line. (i have tried \n
at the end of my FileFormat string and seperately at the end of my
string toFile
.
The code I have for attempting to do this is
public class Surfer
{
protected string SurferFirstName { get; set; }
protected string SurferLastName { get; set; }
protected string SurferDob { get; set; }
protected string SurferCountry { get; set; }
public Surfer() { }
public Surfer(string surferFirstName, string surferLastName, string surferDob, string surferCountry)
{
SurferFirstName = surferFirstName;
SurferLastName = surferLastName;
surferDob = SurferDob;
surferCountry = SurferCountry;
}
public override string ToString()
{
return string.Format("Surfer name: {0} {1} Surferdob: {2} \n Country: {3}", SurferFirstName, SurferLastName, SurferDob, SurferCountry);
}
//for display in the textblock, virtual for inheritance
/* public virtual string vehicleDetails()
{
return string.Format("Make:{0,40} \nModel:{1,40} \nPrice:{2,40} \nYear: {3,40} \nColour: {4,40}, \nMileage: {5,40} \nDescription: {6,40} \nEngine: {7,40}", Make, Model, Price, Year, Colour, Mileage, Description, Engine);
}*/
//for formatting to a text file
public string FileFormat()
{
return string.Format("{0},{1},{2},{3} \n", SurferFirstName, SurferLastName, SurferDob, SurferCountry);
}
}
My code is the WindowAdd.cs
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
//reading in from text boxes
string fName = txtbxFirstName.Text;
string lName = txtbxLastName.Text;
string dob = txtbxDob.Text;
string country = txtbxCountry.Text;
//basic validation
if (fName.Length <2 || lName.Length <2 )
{
MessageBox.Show("first and last name must be entered");
}
else if(dob.Length <10 || dob.Length>10)
MessageBox.Show("Enter dob in the correct format (dd/mm/yyyy)");
else if(country.Length <3)
MessageBox.Show("Enter a valid country");
else
{
try
{
//sets mainWindow
MainWindow mainWindow = new MainWindow();
//creates new surfer class
Surfer newSurfer = new Surfer(fName, lName, dob, country);
//adds to observable collection
mainWindow.surfers.Add(newSurfer);
//uses FileFormat from the Surfer Class to format in order to add to text file
string toFile = newSurfer.FileFormat();
//append to Surfers.Text
File.AppendAllText("Surfers.Text", toFile);
//closes window
this.Close();
}
The result I am getting in the text file is like this
John,Barry,, Happy,Gilmore,,
this is for two attempted entries
The basic validation is working so it is reading in from the xaml okay. I know I could use File.WriteAllLines
and write code to send every element in my observable collection to the text file through a save button, but if possible I would rather do it in a manner similar to the method I am trying, where I save one entry to the textfile at a time without deleting existing elements.
Upvotes: 0
Views: 88
Reputation: 2785
You constructor is assigning variables the wrong direction for Dob and Country. You're assigning the property into the parameter, instead of parameter into the property.
public Surfer(string surferFirstName, string surferLastName, string surferDob, string surferCountry)
{
SurferFirstName = surferFirstName;
SurferLastName = surferLastName;
surferDob = SurferDob;
surferCountry = SurferCountry;
}
For new lines, use Environment.NewLine
as suggested above by Quantic.
Upvotes: 1