Doh!
I really need to verify gradious statements. :-(
Below is from VB 2005 Beta 2 the initialize is outside the Try, however the
Finally still checks for Null/Nothing as the Try block could have set it to
nothing.
So now I'm left wondering where I saw it in IL that it was inside...
Private Sub About_Click(ByVal button As Office.CommandBarButton, ByRef
CancelDefault As Boolean)
Using dialog As New AboutBoxForm
dialog.ShowDialog()
End Using
End Sub
..method private instance void About_Click(class
[office]Microsoft.Office.Core.CommandBarButton button,
bool& CancelDefault) cil managed
{
// Code size 41 (0x29)
.maxstack 2
.locals init ([0] class XmlExportSample.AboutBoxForm dialog,
[1] bool VB$CG$t_bool$S0)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{5A869D0B-6611-11D3-BD2A-0000F80849BD}'
// Source File 'C:\Documents and Settings\jay\My Documents\Visual Studio
2005\Projects\Xml Export Sample\Xml Export Sample\UIController.vb'
//000115: Private Sub About_Click(ByVal button As
Office.CommandBarButton, ByRef CancelDefault As Boolean)
IL_0000: nop
//000116: Using dialog As New AboutBoxForm
IL_0001: nop
IL_0002: newobj instance void XmlExportSample.AboutBoxForm::.ctor()
IL_0007: stloc.0
IL_0008: nop
//000117: dialog.ShowDialog()
.try
{
IL_0009: ldloc.0
IL_000a: callvirt instance valuetype
[System.Windows.Forms]System.Windows.Forms.DialogResult
[System.Windows.Forms]System.Windows.Forms.Form::ShowDialog()
IL_000f: pop
//000118: End Using
IL_0010: nop
IL_0011: leave.s IL_0027
} // end .try
finally
{
IL_0013: ldloc.0
IL_0014: ldnull
IL_0015: ceq
IL_0017: ldc.i4.0
IL_0018: ceq
IL_001a: stloc.1
IL_001b: ldloc.1
IL_001c: brfalse.s IL_0025
IL_001e: ldloc.0
IL_001f: callvirt instance void
[mscorlib]System.IDisposable:
ispose()
IL_0024: nop
IL_0025: nop
IL_0026: endfinally
//000119: End Sub
} // end handler
IL_0027: nop
IL_0028: ret
} // end of method UIController::About_Click
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
message | David,
|| Yes, I didn't want to change the posted code, point out that variables
|| Disposables should be instantiated before the Try blocks in which they
|| disposed, not inside them.
| Sometimes, but not all the times!
|
| Use ILDASM to look at the Using statement, the initialization code is
inside
| the Try block!
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley -
http://www.tsbradley.net
|
|
| message ||
|| message || > David,
|| > Err, Oops...
|| >
|| > Try:
|| >
|| > | Dim f As new Form()
|| > | Try
|| > | 'use f
|| >
|| > Finally
|| >
|| > | f.Dispose()
|| > | End Try
|| >
|| > As you want to Dispose of the variable even if (*especially if*) an
|| > exception is NOT thrown!
|| >
||
|| Yes, I didn't want to change the posted code, point out that variables
|| Disposables should be instantiated before the Try blocks in which they
|| disposed, not inside them.
||
|| David
||
||
|
|