StreamWriter Scoping

  • Thread starter Thread starter Mikep
  • Start date Start date
M

Mikep

Hi --

I'm sort of a newbi with C# and I have a question about scoping -- here's
what I'm trying to do:

StreamWriter swOutput;
if (condition)
{
//assemble a file name
string filename = "MyFile";
swOutput = new StreamWriter(filename)
}
else
{
swOutput.WriteLine("Data"); << using an unitialized variable
}

I can't create the file name until the condition is true and then I write to
the file. But there doesn't appear to be a good place to declare the
StreamWriter where it can be seen by both the true and false conditions. In
the above case, the compiler complains that I'm trying to use an
ininitialized variable.


TIA

Mike P
 
Hi --

I'm sort of a newbi with C# and I have a question about scoping -- here's
what I'm trying to do:

StreamWriter swOutput;
if (condition)
{
//assemble a file name
string filename = "MyFile";
swOutput = new StreamWriter(filename)
}
else
{
swOutput.WriteLine("Data"); << using an unitialized variable
}

I can't create the file name until the condition is true and then I
write to
the file. But there doesn't appear to be a good place to declare the
StreamWriter where it can be seen by both the true and false conditions.
In
the above case, the compiler complains that I'm trying to use an
ininitialized variable.

I don't understand the question. The compiler is exactly correct. You
_are_ trying to use an uninitialized variable.

Your code example is too brief and incomplete for anyone to be able to
understand what you want your code to do, or why you think the code you
posted ought to work. If you want an answer, you'll need to elaborate on
your question. Provide a concise-but-complete code example showing
exactly what you're trying to do and why it doesn't work, along with any
necessary explanatory comments describing why it is you think it ought to
work, or at least how you would like it to work.

Pete
 
Mikep brought next idea :
Hi --

I'm sort of a newbi with C# and I have a question about scoping -- here's
what I'm trying to do:

StreamWriter swOutput;
if (condition)
{
//assemble a file name
string filename = "MyFile";
swOutput = new StreamWriter(filename)
}
else
{
swOutput.WriteLine("Data"); << using an unitialized variable
}

I can't create the file name until the condition is true and then I write to
the file. But there doesn't appear to be a good place to declare the
StreamWriter where it can be seen by both the true and false conditions. In
the above case, the compiler complains that I'm trying to use an
ininitialized variable.


TIA

Mike P

You *could* initialise that StreamWriter to null:
StreamWriter swOutput = null;

then the compiler wouldn't complain. But the code you posted would fail
at *runtime* with a NullReferenceException when 'condition' is false.
As Peter said, you need to provide a more complete example of what you
want to do.

Hans Kesting
 
Mikep said:
Hi --

I'm sort of a newbi with C# and I have a question about scoping -- here's
what I'm trying to do:

StreamWriter swOutput;
if (condition)
{
//assemble a file name
string filename = "MyFile";
swOutput = new StreamWriter(filename)
}
else
{
swOutput.WriteLine("Data"); << using an unitialized variable
}

I can't create the file name until the condition is true and then I write to
the file. But there doesn't appear to be a good place to declare the
StreamWriter where it can be seen by both the true and false conditions. In
the above case, the compiler complains that I'm trying to use an
ininitialized variable.


TIA

Mike P

Is your intent within a loop to check condition in order to open a new
stream, and if condition is false to continue with an old (already
opened) stream?

If so, the logic would look something like this:

StreamWriter swOutput = null;
while (SomethingIsTrue)
{
if (condition)
{
if (swOutput != null)
{
// close up previous file.
swOutput.Flush();
swOutput.Dispose();
}
string filename = "MyFile";
swOutput = new StreamWriter(filename);
}
else
{
if (swOutput != null) swOutput.WriteLine ("Data");
}
}
 
Hello,

If condition is false your code will fail as you'll try to write in a stream
that has not yet been opened (the stream variable won't even be
initialized). Plus if condition is true, you'll open the stream but you
won't write into it...

It seems you have some kind of logic flaw. What are you trying to implement
that goes beyond to just open the stream, write to it and close it ? Do you
want to keep a stream open for a long period of time rather than
opening/closing it as needed ?
 
Family Tree Mike said:
Is your intent within a loop to check condition in order to open a new
stream, and if condition is false to continue with an old (already opened)
stream?

If so, the logic would look something like this:

StreamWriter swOutput = null;
while (SomethingIsTrue)
{
if (condition)
{
if (swOutput != null)
{
// close up previous file.
swOutput.Flush();
swOutput.Dispose();
}
string filename = "MyFile";
swOutput = new StreamWriter(filename);
}
else
{
if (swOutput != null) swOutput.WriteLine ("Data");
}
}

Thanks -- that's what I'm doing... And I never though of initializing the
StreamWriter to null- I'm lots more familiar with C++ and null (well Null)
goes with pointers.

Mike P
 
Thanks -- that's what I'm doing... And I never though of initializing the
StreamWriter to null- I'm lots more familiar with C++ and null (well Null)
goes with pointers.

Well. swOutput is a reference and a reference in C# is really a pointer.

Arne
 
Back
Top