Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "Using" statement translate from C# to VB?

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.

like image 667
Daniel Avatar asked May 20 '09 13:05

Daniel


People also ask

What is the using statement in C# used for?

The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.

How using statement works?

The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.

Does using statement Call dispose?

The using statement guarantees that the object is disposed in the event an exception is thrown. It's the equivalent of calling dispose in a finally block.

What is using in VB net?

Using statement basically marks a boundary for the objects specified inside using () braces. So when code block using (){} (in C#) or Using – End Using (in VB.NET) is exited either after normal execution or some exception cause, the framework invokes the Dispose method of these objects automatically.


1 Answers

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

  • http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx

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 
like image 91
JaredPar Avatar answered Oct 12 '22 04:10

JaredPar