Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. what is Using keyword in VB.net ?

what is Using keyword in VB.net ?

Scheduled Pinned Locked Moved Visual Basic
csharpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xingselex
    wrote on last edited by
    #1

    hello ! can u tell me what is Using keyword in VB.net ? what it do ? and if i want to write a keyword like this in C#, which keyword should i use ?

    0 L 2 Replies Last reply
    0
    • X xingselex

      hello ! can u tell me what is Using keyword in VB.net ? what it do ? and if i want to write a keyword like this in C#, which keyword should i use ?

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      The Using keyword simply sticks a Dispose call at the end of the code. For example:

      Using(sw As New StreamWriter("C:/TestFile.txt"))
      sw.WriteLine("Aloha!")
      End Using

      becomes

      Try
      Dim sw As New StreamWriter("C:/TestFile.txt")

      sw.WriteLine("Aloha!")
      

      Finally
      sw.Dispose()
      End Try

      The keyword in C# is using; it's case sensitive, unlike VB.Net

      Between the idea And the reality Between the motion And the act Falls the Shadow

      C 1 Reply Last reply
      0
      • X xingselex

        hello ! can u tell me what is Using keyword in VB.net ? what it do ? and if i want to write a keyword like this in C#, which keyword should i use ?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The "using" Keyword in C#[^] I THINK IT'S HALPFUL FOR U. BEST REGARD

        If you can think then I Can.

        C 1 Reply Last reply
        0
        • 0 0x3c0

          The Using keyword simply sticks a Dispose call at the end of the code. For example:

          Using(sw As New StreamWriter("C:/TestFile.txt"))
          sw.WriteLine("Aloha!")
          End Using

          becomes

          Try
          Dim sw As New StreamWriter("C:/TestFile.txt")

          sw.WriteLine("Aloha!")
          

          Finally
          sw.Dispose()
          End Try

          The keyword in C# is using; it's case sensitive, unlike VB.Net

          Between the idea And the reality Between the motion And the act Falls the Shadow

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          It does more for you than just call Dispose in the manner in which you describe. It also wraps it in a Try/Finally block too so that Dispose is always called even in the event that an exception is raised. This is where its real usefulness lies. It is much easier to write the Using block that it would two write all the corresponsing Try/Finally stuff to do the same thing.

          User group: Scottish Developers Blog: Can Open... Worms? Everywhere! Quote: Man who stand on hill with mouth open wait long time for roast duck to drop in.

          0 1 Reply Last reply
          0
          • L Lost User

            The "using" Keyword in C#[^] I THINK IT'S HALPFUL FOR U. BEST REGARD

            If you can think then I Can.

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            eg_Anubhava wrote:

            I THINK IT'S HALPFUL FOR U.

            I NOT DEAF! PLEASE STOP SHOUTING! It is very rude to use all caps.

            User group: Scottish Developers Blog: Can Open... Worms? Everywhere! Quote: Man who stand on hill with mouth open wait long time for roast duck to drop in.

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              It does more for you than just call Dispose in the manner in which you describe. It also wraps it in a Try/Finally block too so that Dispose is always called even in the event that an exception is raised. This is where its real usefulness lies. It is much easier to write the Using block that it would two write all the corresponsing Try/Finally stuff to do the same thing.

              User group: Scottish Developers Blog: Can Open... Worms? Everywhere! Quote: Man who stand on hill with mouth open wait long time for roast duck to drop in.

              0 Offline
              0 Offline
              0x3c0
              wrote on last edited by
              #6

              Ah, I didn't know that. Thanks for the update; I've updated my example code to take that into account

              Between the idea And the reality Between the motion And the act Falls the Shadow

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups