473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
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
Jan 9 '07 #2
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
<"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
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
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
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
<"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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...
2
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...
1
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...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
6
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...
2
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...
40
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...
2
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.