471,315 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Want to create either a stream or a file

I am reworking a class that generates source code. I want to add a
"preview" function, much like the refactor dialog in vs2005. To facilitate
this, I have changed what used to be my "writeToFile" method to "generate" -
the plan is, I will generate the text in memory, then I can either show it
in a control or pass it to the method that does the file IO stuff.

I'm trying to determine the best class' to use for this, I was hoping there
was something that would let me build the string, then pass it to a writer
class, but I haven't found it yet. Currently I'm using StringWriter to
generate the source, then when I want to write the code, I use
StreamWriter.Write() passing it my StringWriter.ToString();

This seems ugly to me. Is there another way that would be cleaner? I was
hoping for something like setting a StreamWriter's internal data to my
StringWriter, or casting a StringWriter to a StreamWriter... something like
that. They seem so closely related.

Any ideas?

Thanks for reading,
Steve
Dec 13 '05 #1
1 1223
Steve,

I would have your generate method take a TextWriter. That way, if you
want it in a string that you can get later, you can pass a StringWriter. If
you want to write it to a file, then you can use a StreamWriter instance
with a FileStream backing it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:ut**************@TK2MSFTNGP11.phx.gbl...
I am reworking a class that generates source code. I want to add a
"preview" function, much like the refactor dialog in vs2005. To facilitate
this, I have changed what used to be my "writeToFile" method to
"generate" - the plan is, I will generate the text in memory, then I can
either show it in a control or pass it to the method that does the file IO
stuff.

I'm trying to determine the best class' to use for this, I was hoping
there was something that would let me build the string, then pass it to a
writer class, but I haven't found it yet. Currently I'm using
StringWriter to generate the source, then when I want to write the code, I
use StreamWriter.Write() passing it my StringWriter.ToString();

This seems ugly to me. Is there another way that would be cleaner? I was
hoping for something like setting a StreamWriter's internal data to my
StringWriter, or casting a StringWriter to a StreamWriter... something
like that. They seem so closely related.

Any ideas?

Thanks for reading,
Steve

Dec 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Brian W. Smith | last post: by
3 posts views Thread by Peter | last post: by
2 posts views Thread by matt | last post: by
1 post views Thread by tofu.captain | last post: by
6 posts views Thread by Parrot | last post: by
4 posts views Thread by nano2k | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.