Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
All:
C#
private void AppendData(string strData, string strFileName)
        {
            FileStream fStream;
            StreamWriter sWriter;
            try
            {
                fStream = new FileStream(strFileName, FileMode.Append);
                sWriter = new StreamWriter(fStream);
                sWriter.WriteLine(strData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sWriter.Close();
                sWriter.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
        }

The above code tells during compiling that "Use of unassigned local variable. It points to sWriter and fStream inside the finally block". Why I did it this way was, I read in a forum that .NET does not require us to explicitly initialize a variable. Is my understanding wrong ?

The code works fine if I initialize it to NULL.
C#
private void AppendData1(string strData, string strFileName)
        {
            FileStream fStream = null;
            StreamWriter sWriter = null;
            try
            {
                fStream = new FileStream(strFileName, FileMode.Append);
                sWriter = new StreamWriter(fStream);
                sWriter.WriteLine(strData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sWriter.Close();
                sWriter.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
        }

Can you throw some light here...
Posted
Updated 26-Jun-12 18:32pm
v2
Comments
OriginalGriff 27-Jun-12 0:45am    
Answer updated

If the compiler can see any route through the software such that the variable does not get a value, it complains. Such a route exists:
C#
FileStream fStream;
StreamWriter sWriter;
try
   {
   fStream = new FileStream(strFileName, FileMode.Append);
This line causes an exception - fStream is not modified.
These lines are skipped:
C#
sWriter = new StreamWriter(fStream);
sWriter.WriteLine(strData);
}
Execution continues here:
C#
catch (Exception ex)
   {
   MessageBox.Show(ex.Message);
   }
Then the finally block is executed:
C#
finally
   {

This is the first reference to sWriter since it was declared.
C#
sWriter.Close();
sWriter.Dispose();
This is the first reference to fStream since it was declared.
C#
fStream.Close();
fStream.Dispose();
}


[edit]
Setting it to null gets rid of the compilation problem, but introduces another: your finally block will cause an exception if your try/catch is triggered.
You need to check for null in each before you try to Close or Dispose. I would suggest rewriting this as:
C#
private void AppendData(string strData, string strFileName)
        {
            try
            {
                using (FileStream fStream = new FileStream(strFileName, FileMode.Append))
                {
                     using (StreamWriter sWriter = new StreamWriter(fStream))
                     {
                          sWriter.WriteLine(strData);
                     }
                 }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

- OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
AmitGajjar 27-Jun-12 1:43am    
5+ for edit block, best way of disposing stream. also see my answer for variable initialization. thanks.
Hi,

Although the Edited code by OriginalGiff is perfect. use of using is the best way to dispose any of the variable. but in case you need to initialize your variable in the beginning then don't assign null, instead use the default value of that type.

say for example, if we have our custom class Customer , we have implemented IDisposable with our class and if you want to use finally block then good practice is to use like,

good
C#
Customer newCustomer = default(Customer); 

bad
C#
Customer newCustomer = null;


Reason is if your class is changed to not Nullable then you need to change everywhere. instead above will be helpful to you in future.

And as far as Disposing is concern follow the way of OriginalGriff.

Thanks
-Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900