C# streamwriter problem

Started by
1 comment, last by Bronco78th 13 years, 1 month ago
Hey guys,

Latest minor problem....at least this one is minor.

I have this code.

using (StreamWriter gentxt = new StreamWriter(saveFileDialog1.FileName))
{
for (int i = 0; i < contactlist.Count; i++)
{
gentxt.WriteLine(("Name:"),contactlist.Name);
gentxt.WriteLine("Mobile:",contactlist.Mobile);
gentxt.WriteLine(contactlist.Homephone);
}


That code generates this as a text document.


Name:
Mobile:
54346553456534[/quote]

oviously I want something like.

Name: John
Mobile: 432432565464
Homephone: 87998979969
Etc

Can I only have 1 item (argument) per Writeline or is there another way of doing it?

Cheers
John
Advertisement

Hey guys,

Latest minor problem....at least this one is minor.

I have this code.

using (StreamWriter gentxt = new StreamWriter(saveFileDialog1.FileName))
{
for (int i = 0; i < contactlist.Count; i++)
{
gentxt.WriteLine(("Name:"),contactlist.Name);
gentxt.WriteLine("Mobile:",contactlist.Mobile);
gentxt.WriteLine(contactlist.Homephone);
}


That code generates this as a text document.


Name:
Mobile:
54346553456534


oviously I want something like.

Name: John
Mobile: 432432565464
Homephone: 87998979969
Etc

Can I only have 1 item (argument) per Writeline or is there another way of doing it?

Cheers
John
[/quote]

Only the first argument is for strings. Use something like:

gentxt.WriteLine("Name:" + contactlist.Name);

WriteLine(String, Object, Object)Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Hey Slynk

Cheers for the hasty reply.

Knew it had to be soemthing simple.

I think I tried (("Name:")+ contactlist.Name) but not without the brackets....the only version I seemed to have overlooked was the correct one. <_<

Many thanks anyway.

John

This topic is closed to new replies.

Advertisement