473,407 Members | 2,326 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,407 software developers and data experts.

Call Close in Using Block with StreamWriter

I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
Apr 7 '08 #1
5 2554
On Apr 7, 10:53 pm, JohnMSyrasoft
<JohnMSyras...@discussions.microsoft.comwrote:
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
As far as i know, "End Using" statement is enough to dispose resource.
No need to use close method in that case.
Apr 7 '08 #2
On 2008-04-07, JohnMSyrasoft <Jo***********@discussions.microsoft.comwrote:
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
It is closed when the using block exits - even if there is an error,
that is the point of a using block. Calling close explicitly, just makes
for redundant code.

--
Tom Shelton
Apr 7 '08 #3

"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Calling close explicitly, just makes for redundant code.

--
Tom Shelton
Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott
Apr 7 '08 #4
On 2008-04-07, Scott M. <sm**@nospam.nospamwrote:
>
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Calling close explicitly, just makes for redundant code.

--
Tom Shelton

Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott

IMHO, that would be a rare case, and one that I would suggest is
probably in need of refactoring. In general, with files the rule is
get in, get what you need, and get out. No, in general, I would stand
by my original post.

--
Tom Shelton
Apr 7 '08 #5
IMHO, I don't think it's as rare as you may think. If I'm going to need to
re-open the stream after I close it, I wouldn't want it disposed yet, but I
would want it closed while I'm not using it.
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:eO**************@TK2MSFTNGP03.phx.gbl...
On 2008-04-07, Scott M. <sm**@nospam.nospamwrote:
>>
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Calling close explicitly, just makes for redundant code.

--
Tom Shelton

Not necessarially. You may be done using the object, but not have hit
the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you
are
done using it and use "End Using" as a fail-safe.

-Scott


IMHO, that would be a rare case, and one that I would suggest is
probably in need of refactoring. In general, with files the rule is
get in, get what you need, and get out. No, in general, I would stand
by my original post.

--
Tom Shelton

Apr 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: Daniel | last post by:
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same...
4
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write,...
1
by: Michael R. Pierotti | last post by:
I have a app I have written that when the program is opened it logs out to a logfile. What I can't figure out is that "X" button at the top. If a user clicks on that instead of my menu item close...
4
by: hharry | last post by:
Hi All, I have a simple web service: <WebMethod()> _ Public Function ReSample(ByVal sInput As Integer) As String
1
by: hharry | last post by:
Hi All, I have asimple web service: <WebMethod()> _ Public Function ReSample(ByVal sInput As Integer) As String
7
by: Eran.Yasso | last post by:
Hi, In the MSDN the sample doesn't use the close() method. But I know that in most languages you do need to use the close() method after reading and writing to a file. from MSDN: public...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
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,...
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.