Could someone explain me what is wrong with this code ?
I gives me a compile error:
Error 1 Use of unassigned local variable 'fileStreamObje ct' C:\Documents and
Settings\Bart\L ocal Settings\Applic ation Data\Temporary
Projects\Window sApplication1\e mployeeReader.c s 22 17 WindowsApplicat ion1
The code is:
public void Write(string filename, Employee emp)
{
FileStream fileStreamObjec t;
try
{
fileStreamObjec t = new FileStream(file name,
FileMode.Create );
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, emp);
}
finally
{
fileStreamObjec t.Close(); <-------- gives an error !!!
}
}
Thanks a lot
Bart 8 1648
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 'fileStreamObje ct' C:\Documents
and Settings\Bart\L ocal Settings\Applic ation Data\Temporary
Projects\Window sApplication1\e mployeeReader.c s 22 17 WindowsApplicat ion1
The code is:
public void Write(string filename, Employee emp)
{
FileStream fileStreamObjec t;
try
{
fileStreamObjec t = new FileStream(file name,
FileMode.Create );
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, emp);
}
finally
{
fileStreamObjec t.Close(); <-------- gives an error !!!
}
}
Two things, change your declaration to as follows:
FileStream fileStreamObjec t = null;
Then in your finally wrap your close in a null check:
if (fileStreamObje ct != null)
fileStreamObjec t.Close();
--
Tom Porterfield
Two things, change your declaration to as follows:
>
FileStream fileStreamObjec t = null;
Then in your finally wrap your close in a null check:
if (fileStreamObje ct != null)
fileStreamObjec t.Close();
--
Tom Porterfield
That does the trick......than ks for the quick reply :)
Bart
<"Bart" <@>wrote:
Two things, change your declaration to as follows:
FileStream fileStreamObjec t = null;
Then in your finally wrap your close in a null check:
if (fileStreamObje ct != null)
fileStreamObjec t.Close();
--
Tom Porterfield
That does the trick......than ks for the quick reply :)
That does the trick, but a better solution would be to use a "using"
statement:
using (Stream fileStream = new FileStream(file name, FileMode.Create ))
{
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, emp);
}
The "using" statement automatically calls Dispose for you, creating a
try/finally itself.
--
Jon Skeet - <sk***@pobox.co m> 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 fileStreamObjec t;
try
{
fileStreamObjec t = new FileStream(file name,
FileMode.Create );
If this line fails, then there is no FileStream object....
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, emp);
}
finally
{
fileStreamObjec t.Close(); <-------- gives an error !!!
....and this isn't meaningful when that happens.
The using block is best, another fix is to move the fileStreamObjec t
initialization above the try block:
FileStream fileStreamObjec t = new FileStream(file name, FileMode.Create ); //
if this fails, the whole try/catch isn't executed
try {
}
}
Thanks a lot
Bart
using (Stream fileStream = new FileStream(file name, FileMode.Create ))
{
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, 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(file StreamObject, emp);
Should be : binaryFormatter .Serialize(file Stream, 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(file name, FileMode.Create ))
{
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(file StreamObject, 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(file StreamObject, emp);
Should be : binaryFormatter .Serialize(file Stream, emp);
Indeed. Cut and paste error, I'm afraid.
--
Jon Skeet - <sk***@pobox.co m> 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 below to quickly design,
generate, and deploy a great portion of your real-life N-
Tier application.
For free evaluation download please visit
http://www.alachisoft.com/download.htm
|
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 from http://nds.nokia.com/uaprof/N6600r100.xml
but nokia mobile browser cannot identify the converted file(XHTML1.0).
Is there something wrong with my procedure.
|
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
---------------
1. stop all db2 services
2. install db2 8.1 (I guess this should automatically upgrade the
earlier version?)
3. install the latest fixpak for 8.1
|
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 contained one or more
zip files in each. Many of the zipfiles had the same filename (which is
why they had previously been stored in separate directories). I wanted
to bring all of the zip files (several hundrd in total) down to the
common parent...
| |
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 single job. The first two steps
are set to "Goto next step" on success; the last step is set to "Quit with
success."
The job runs every night. However, only the first step/procedure is
executed. Also, the first step has a green flag next to its...
|
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
anything like that in three years. I want to get back into the
programming scene so I have a question: Which should I relearn
first, C or C++? I have forgotten a lot about both languages so
please tell me which i should relearn first!
...
|
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 Steps. Cannot find
supported Web browser. Explanation: DB2 First Steps requires a supported
Web browser.' But Internet Explorer 7 is set as the default browser !
On another server with Internet Explorer 6, and DB2 9 ESE fixpack 1, 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 the
error:
A) The connection has been closed on a receive : Unable to read data
from the transport connection: An existing connection was forcibly
closed by the remote host.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |