First steps in C#......could someone help please ?

  • Thread starter Thread starter Bart
  • Start date Start date
B

Bart

Could someone explain me what is wrong with this code ?

I gives me a compile error:

Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and
Settings\Bart\Local Settings\Application Data\Temporary
Projects\WindowsApplication1\employeeReader.cs 22 17 WindowsApplication1


The code is:

public void Write(string filename, Employee emp)
{
FileStream fileStreamObject;
try
{
fileStreamObject = new FileStream(filename,
FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, emp);
}
finally
{
fileStreamObject.Close(); <-------- gives an error !!!
}
}

Thanks a lot

Bart
 
Bart said:
Could someone explain me what is wrong with this code ?

I gives me a compile error:

Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents
and Settings\Bart\Local Settings\Application Data\Temporary
Projects\WindowsApplication1\employeeReader.cs 22 17 WindowsApplication1


The code is:

public void Write(string filename, Employee emp)
{
FileStream fileStreamObject;
try
{
fileStreamObject = new FileStream(filename,
FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, emp);
}
finally
{
fileStreamObject.Close(); <-------- gives an error !!!
}
}

Two things, change your declaration to as follows:

FileStream fileStreamObject = null;

Then in your finally wrap your close in a null check:

if (fileStreamObject != null)
fileStreamObject.Close();
 
Two things, change your declaration to as follows:
FileStream fileStreamObject = null;

Then in your finally wrap your close in a null check:

if (fileStreamObject != null)
fileStreamObject.Close();

That does the trick......thanks for the quick reply :)

Bart
 
That does the trick......thanks for the quick reply :)

That does the trick, but a better solution would be to use a "using"
statement:

using (Stream fileStream = new FileStream(filename, FileMode.Create))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, emp);
}

The "using" statement automatically calls Dispose for you, creating a
try/finally itself.
 
Here's the reason the compiler complained:
The code is:

public void Write(string filename, Employee emp)
{
FileStream fileStreamObject;
try
{
fileStreamObject = new FileStream(filename,
FileMode.Create);
If this line fails, then there is no FileStream object....

BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, emp);
}
finally
{
fileStreamObject.Close(); <-------- gives an error !!!
....and this isn't meaningful when that happens.

The using block is best, another fix is to move the fileStreamObject
initialization above the try block:

FileStream fileStreamObject = new FileStream(filename, FileMode.Create); //
if this fails, the whole try/catch isn't executed
try {
 
using (Stream fileStream = new FileStream(filename, FileMode.Create))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, emp);
}

The "using" statement automatically calls Dispose for you, creating a
try/finally itself.

Thanks for your reply

Does this mean I don't have to close the stream myself ?

by the way
I guess : binaryFormatter.Serialize(fileStreamObject, emp);
Should be : binaryFormatter.Serialize(fileStream, emp);

right ?

Bart
 
Thanks for your reply

Does this mean I don't have to close the stream myself ?

Yup. You do need to remember to use the "using" statement though :) The
same goes for any type implementing IDisposable.
by the way
I guess : binaryFormatter.Serialize(fileStreamObject, emp);
Should be : binaryFormatter.Serialize(fileStream, emp);

Indeed. Cut and paste error, I'm afraid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top