By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jan 9 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Bart wrote:
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();
--
Tom Porterfield
Jan 9 '07 #2

P: n/a
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();
--
Tom Porterfield
That does the trick......thanks for the quick reply :)

Bart
Jan 9 '07 #3

P: n/a
<"Bart" <@>wrote:
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();
--
Tom Porterfield

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '07 #4

P: n/a
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 {

}
}

Thanks a lot

Bart

Jan 9 '07 #5

P: n/a
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

Jan 9 '07 #6

P: n/a
Hello Bart,

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

yep, the using statement will dispose the object (calls Dispose method) when
it is out of scope http://msdn2.microsoft.com/en-us/library/yh598w02.aspx

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
Jan 9 '07 #7

P: n/a
<"Bart" <@>wrote:
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 ?
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '07 #8

P: n/a
yep, the using statement will dispose the object (calls Dispose method)
when it is out of scope
http://msdn2.microsoft.com/en-us/library/yh598w02.aspx
Hi Michael ,

Thanks for the reply....I think I am going to like C# and .net

Bart
Jan 9 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.