I understand that the title may be a little bit confusing but it tells exactly what I will teach you in this tutorial, how to use Using in VB.NET. Using is used when using temporary recourses for a part of your code. An example is to use Using to create a new Graphics, then we can use this Graphics inside the block which will start with the Using and the with End Using. When reaching the end of the block our Graphics(in the example, it could be something else too) is disposed. So this will allow us to easily create a temporary object we only want to use a little time and then automatically remove it when we don't need it. This means we can only access this object within the Using block. Note that all objects that are used as Using must be disposable, so for example Integers and Strings won't work. So the code for the above example could look something like the code below, not that I'm only using Graphics as an example, if you actually want to learn about it read this tutorial instead:
[highlight=VB.NET] Dim Img As New Bitmap(50, 50)
Using g As Graphics = Graphics.FromImage(Img)
g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))
g.FillEllipse(Brushes.Blue, New Rectangle(5, 5, 20, 20))
g.FillEllipse(Brushes.Red, New Rectangle(25, 25, 20, 20))
End Using
Me.BackgroundImage = Img[/highlight]
So what the code do is that it uses Using to create a Graphics called g. Then we can use g as we want inside the Using block. Which we in the above example do three times. But then the Graphics have done what it was supposed to do and therefor we don't need it anymore, so at End Using the Graphics will be disposed which means it will be completely removed.
Since this object is only created because we want to use it, it's automatically made ReadOnly, this means we can't change the value of the Using's variable, for example this will NOT work:
[highlight=VB.NET] Using stream As New IO.FileStream("C:\myDirectory\myFile.extension", IO.FileMode.CreateNew)
stream = New IO.FileStream("C:\myDirectory\myOtherFile.extensio n", IO.FileMode.CreateNew)
End Using[/highlight]
And what more is special with ReadOnly variables? Since they are ReadOnly we can't change it's value later as I showed above, this means we have to set its value in the beginning, so this example below will NOT work either:
[highlight=VB.NET] Using stream As IO.FileStream
End Using[/highlight]
Something that is possible is to add as many resources to the same Using as you want, they will all be initialized at the beginning and they will all be disposed at the end, the example below shows a Using using both a Graphics and a SaveFileDialog:
[highlight=VB.NET] Using g As Graphics = Graphics.FromImage(Img), Save As New SaveFileDialog()
g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))
Save.ShowDialog()
Img.Save(Save.FileName)
End Using[/highlight]
So when using Using you'll never forget to the dispose the object when you not need it anymore, furthermore you should make a variable ReadOnly if you'll only use it for reading, we won't forget to add this either since it's automatically included. Another thing is that you can't use your disposed objected by accident since the object could only be accessed from within the Using block, and also it's easier to see where you're using your objects and also that you won't use them any more later on which makes the code easier to read. This was everything for me and my tutorial with the strange name "Using Using VB.NET". Have a fun time coding and Bye![]()
When I first read the title I thought you made a typo!
+rep
Yes I understood it could be like that and therefor I started with an explanation. Thanks![]()
I can see that being very handy. +rep
I just started with VB. I need help.
Anyone can help me?
What do you need help with? Post a new thread in the VB section, then it's higher chance you got better help.
What is data validation and comment?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks