56

For example:

BitmapImage bitmap = new BitmapImage();

byte[] buffer = GetHugeByteArray(); // from some external source
using (MemoryStream stream = new MemoryStream(buffer, false))
{
    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
    bitmap.Freeze();
}

Can you tell me any more about using?

Edit:

As was discussed in the comments of JaredPar's post, this question is more concerned with an implementation of Using in VS2003. It was pointed out that Using was not introduced until .NET 2.0 (VS2005). JaredPar posted an equivalent workaround.

Daniel
  • 10,864
  • 22
  • 84
  • 115
  • 2
    Search for "using keyword C#" or some variation of. – James Gregory May 20 '09 at 13:20
  • 1
    From the **Visual Basic Programming Guide:** [How to: Dispose of a System Resource (Visual Basic)](http://msdn.microsoft.com/en-us/library/wydd5hkd.aspx) – DavidRR May 22 '14 at 14:32
  • 1
    For those looking for the equivalent of the using directive ie. using System.Net; the answer is here http://stackoverflow.com/questions/4134472/vb-net-equivalent-to-cs-using-directive – Martin Brown Jul 15 '14 at 16:05
  • 1
    Using in VB.net can also do compound entries where C# requires two separate lines. Also, implied types can be used, i.e.: Using x = New MemoryStream(buffer1), y = New MemoryStream(buffer2) ..... End Using – Brain2000 Sep 07 '18 at 20:45

5 Answers5

74

Using has virtually the same syntax in VB as C#, assuming you're using .NET 2.0 or later (which implies the VB.NET v8 compiler or later). Basically, just remove the braces and add a "End Using"

Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Using stream As New MemoryStream(buffer, false)
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
End Using

You can get the full documentation here

EDIT

If you're using VS2003 or earlier you'll need the below code. The using statement was not introduced until VS 2005, .NET 2.0 (reference). Thanks Chris!. The following is equivalent to the using statement.

Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Dim stream As New MemoryStream(buffer, false)
Try
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
Finally
    DirectCast(stream, IDisposable).Dispose()
End Try
monami
  • 594
  • 2
  • 9
  • 32
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Which version of VB was this added? I'm working with some legacy code and Visual Studio 2003 doesn't seem to be picking up on it. – Daniel May 20 '09 at 13:21
  • @hypoxide, the using statement has been around since the original VB.Net AFAIK. I changed the code to get rid of a couple of items that weren't available till later versions. Can you try that and if you see any errors add them here? – JaredPar May 20 '09 at 13:23
  • 14
    The Using statement in VB was not introduced until VS2005. It is not available in prior versions. – Chris Dunaway May 20 '09 at 13:27
  • 1
    Chris is correct: http://en.wikipedia.org/wiki/Visual_Basic_2005#Visual_Basic_2005_.28VB_8.0.29 – JaredPar May 20 '09 at 13:44
  • 2
    Also note that for Namespace references and alias declaration C# using is the same as VB's Imports. – TGnat May 20 '09 at 14:14
5

Its important to point out that using is actually compiled into various lines of code, similar to lock, etc.

From the C# language specification.... A using statement of the form

using (ResourceType resource = expression) statement

corresponds to one of two possible expansions. When ResourceType is a value type, the expansion is

{
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        ((IDisposable)resource).Dispose();
    }
}

Otherwise, when ResourceType is a reference type, the expansion is

{
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        if (resource != null) ((IDisposable)resource).Dispose();
    }
}

(end language specification snippet)

Basically, at compile time its converted into that code. There is no method called using, etc. I tried to find similar stuff in the vb.net language specification but I couldn't find anything, presumably it does the exact same thing.

Daniel
  • 10,864
  • 22
  • 84
  • 115
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
3

That would be something like this:

Dim bitmap As New BitmapImage()
Dim buffer As Byte() = GetHugeByteArray()
Using stream As New MemoryStream(buffer, False)
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
End Using
Bojan Resnik
  • 7,320
  • 28
  • 29
3

The key point is that the class being "used" must implement the IDisposable interface.

LunaCrescens
  • 540
  • 3
  • 10
  • 20
2

Seems like using(C#) and Using (VB) have an extremely important difference. And at least for me now, it can defeat the purpose of Using.

Imports System.IO
Class Program

    Private Shared sw As StreamWriter

    Private Shared Sub DoSmth()
        sw.WriteLine("foo")
    End Sub

    Shared Sub Main(ByVal args As String())
        Using sw = New StreamWriter("C:\Temp\data.txt")
            DoSmth()
        End Using
    End Sub
End Class

You'll get NullReferenceException as in VB Using redefines the member class variable while in C# it doesn't!

Of course, maybe I missing something..

jmoreno
  • 12,752
  • 4
  • 60
  • 91
Alex
  • 2,469
  • 3
  • 28
  • 61
  • 1
    it doesn't redefine the member variable, it overrides it within the scope of the method. It still exists, and has the same name – jmoreno May 24 '16 at 19:20