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 8 1638
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
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
<"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
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
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
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
<"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: M.sajjad |
last post by:
Five Steps to Rapid Development with TierDeveloper 3.0
Unlock the power of rapid development when you use
TierDeveloper from AlachiSoft in your N-Tier application
development. Follow the steps...
|
by: mike |
last post by:
regards:
I follow the following steps to converting from HTML to XHTML
http://webpageworkshop.co.uk/main/xhtml_converting
My parser is http://htmlparser.sourceforge.net/
Xhtml version is 1.0...
|
by: sea |
last post by:
Hi, I have db2 workgroup 7.2 and am planning to upgrade to 8.1
workgroup server edition. Could someone please tell me if the order in
which I do this as listed below is correct?
ON THE SERVER...
|
by: spasmous |
last post by:
main()
{
float * f;
initialize_f(f);
// ...use f for processing
free(f);
}
|
by: planetthoughtful |
last post by:
Hi All,
I've written my first piece of practical Python code (included below),
and would appreciate some comments. My situation was that I had a
directory with a number of subdirectories that...
|
by: Neil |
last post by:
I have three stored procedures that need to run nightly in SQL 7. The three
procedures are not related; but to keep the procedures from running at the
same time, I placed them as three steps of a...
|
by: dydx13 |
last post by:
Hello,
I'm currently attending a university majoring in Computer Science.
I took C and C++ at a community college and got an A in both
classes. That was three years ago. I have not programmed or...
|
by: Odd Bjørn Andersen |
last post by:
Is there an issue with fixpack 1 on DB2 9 ESE (on Windows) and First Steps?
After I applied fixpack 1 I am not able to start First Steps, but get the
error message 'DBI1435E Error Opening DB2 First...
|
by: Doug Ferguson |
last post by:
I am using a webservice client that was created from a WSDL file
in .Net 1.1.
The client ALWAYS works the first time I call it.
The second call returns one of two exceptions. It either returns...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |