Sign in to follow this  
Bronco78th

C# streamwriter problem

Recommended Posts

Hey guys,

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

I have this code.

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

That code generates this as a text document.

[quote]
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

Share this post


Link to post
Share on other sites
[quote name='Bronco78th' timestamp='1298328319' post='4777265']
Hey guys,

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

I have this code.

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

That code generates this as a text document.

[quote]
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
[/quote]

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

[code]gentxt.WriteLine("Name:" + contactlist[i].Name);[/code]

[url="http://msdn.microsoft.com/en-us/library/aakt1eab.aspx"]WriteLine(String, Object, Object)[/url]Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

Share this post


Link to post
Share on other sites
Hey Slynk

Cheers for the hasty reply.

Knew it had to be soemthing simple.

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

Many thanks anyway.

John

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this