Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c# Stream Writer output

StreamWriter sWrite = new StreamWriter("Data.txt", true);
 string strWrite = txtID.Text + ","
                              + txtName + ","
                              + txtAdress;`enter code here`
            sWrite.WriteLine(strWrite);
            sWrite.Close();
            MessageBox.Show("Person Added");

this is my code, why the output looks like this?

(System.Windows.Forms.TextBox, Text: Yasser,System.Windows.Forms.TextBox, Text: Jeddah,202)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you want to write the contents of a TextBox to a string, you need to use the TextBox.Text property to access the content.

In your attempt you have used the Text property of the txtID TextBox control correctly, but not the others. The default output of any object when you concatenate it into a string is to call the .ToString() method on that object, by default this will return the name of the type of that object. TextBox however overrides this base and returns the name of the type as well as the content of the default property which is the .Text property. That is why we see "System.Windows.Forms.TextBox, Text: Jeddah" in the output.

We do not need to assume that those controls are textboxes, this output confirms it for us. Therefore, we would expect the following code to produce output similar to your expectations:

string strWrite = txtID.Text + ","
                    + txtName.Text + ","
                    + txtAdress.Text;

I can’t explain why your example output text is in the reverse order though, so I doubt that this output matches the code you have posted as it is.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading