Call Close in Using Block with StreamWriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sm9obk1TeXJhc29mdA==?=

    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.WriteL ine(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.
  • kimiraikkonen

    #2
    Re: Call Close in Using Block with StreamWriter

    On Apr 7, 10:53 pm, JohnMSyrasoft
    <JohnMSyras...@ discussions.mic rosoft.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.WriteL ine(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.

    Comment

    • Tom Shelton

      #3
      Re: Call Close in Using Block with StreamWriter

      On 2008-04-07, JohnMSyrasoft <JohnMSyrasoft@ discussions.mic rosoft.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.WriteL ine(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

      Comment

      • Scott M.

        #4
        Re: Call Close in Using Block with StreamWriter


        "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
        news:%238dG2qOm IHA.5684@TK2MSF TNGP03.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


        Comment

        • Tom Shelton

          #5
          Re: Call Close in Using Block with StreamWriter

          On 2008-04-07, Scott M. <smar@nospam.no spamwrote:
          >
          "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
          news:%238dG2qOm IHA.5684@TK2MSF TNGP03.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

          Comment

          • Scott M.

            #6
            Re: Call Close in Using Block with StreamWriter

            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" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
            news:eOKEk0PmIH A.3512@TK2MSFTN GP03.phx.gbl...
            On 2008-04-07, Scott M. <smar@nospam.no spamwrote:
            >>
            >"Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
            >news:%238dG2qO mIHA.5684@TK2MS FTNGP03.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

            Comment

            Working...