Imran
Mon Sep 27 16:18:32 CDT 2004
Interesting. I guess this should be a useful addition in VB2005.
Thanks for the info, Jay.
Imran.
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:%23pw9tfLpEHA.1308@TK2MSFTNGP14.phx.gbl...
> Imran,
> > Btw, is the 'Using' keyword already in VB2005 Beta1?
>
> Yes. You can do something like:
>
> Using stream As New System.IO.FileStream("myfile.txt",
> IO.FileMode.Open)
> Using reader As New System.IO.StreamReader(stream)
> Dim line As String
> line = reader.ReadLine()
> Do Until line Is Nothing
> ' do something with line
> line = reader.ReadLine()
> Loop
> End Using
> End Using
>
> There's a way to collapse the above to a single statement, however I do
not
> have that handy...
>
> For details see:
>
http://msdn2.microsoft.com/library/htd05whh.aspx
>
> and:
>
>
http://msdn2.microsoft.com/library/wydd5hkd.aspx
>
>
> Hope this helps
> Jay
>
> "Imran Koradia" <nospam@microsoft.com> wrote in message
> news:uExX37JpEHA.3464@TK2MSFTNGP14.phx.gbl...
> > Yup - the overloaded version does make it more complete. And yes I
> > absolutely agree that compile time check is the preferred way. Sounds
good
> > to me :) Btw, is the 'Using' keyword already in VB2005 Beta1?
> >
> > Thanks!
> > Imran.
> >
> > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in
message
> > news:OTX1$nJpEHA.3396@tk2msftngp13.phx.gbl...
> >> Imran,
> >> > Why would the version I sent cause a runtime exception ?!? In fact it
> > will
> >>
> >> > Sub DisposeObject(ByVal DisposableObject As Object)
> >> > If Not DisposableObject Is Nothing Then
> >> > If TypeOf DisposableObject Is IDisposable Then
> >> > DirectCast(DisposableObject, IDisposable).Dispose()
> >> > Else
> >> > ' do nothing..or throw some error..whatever..
> >> > End If
> >> > End If
> >> > End Sub
> >>
> >> Your "or throw some error" comment in your source (if implemented).
Which
> >> will occur for actual objects that do not implement IDisposable.
> >> Unfortunately my sample throws an exception for Nothing also.
> >>
> >> My point is more that you are relying entirely on runtime error
checking,
> >> IMHO compile time error checking is preferred even with Option Strict
> >> Off!
> >>
> >> Which is why, looking back (thinking back) when I played with a
> >> DisposeObject routine I overloaded the routine. The As Object version
> >> performs mostly like yours while the As IDisposable performs like
Joops.
> >>
> >> Something like:
> >>
> >> Sub DisposeObject(ByVal DisposableObject As Object)
> >> If Not DisposableObject Is Nothing Then
> >> DirectCast(DisposableObject, IDisposable).Dispose()
> >> End If
> >> End Sub
> >>
> >> Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
> >> If Not DisposeAbleObject Is Nothing Then
> >> DisposeAbleObject.Dispose()
> >> End If
> >> End Sub
> >>
> >> For strongly typed variables the As IDisposable version will be called,
> >> no
> >> casting needed, for loosely typed variables the As Object version will
be
> >> called, the DirectCast will raise an exception if the object is not
> >> IDisposable, I would use TypeOf as you did if I actually wanted to
avoid
> > the
> >> exception (I don't have my project handy where I was using a
> >> DisposeObject
> >> routine).
> >>
> >> Hope this helps
> >> Jay
> >>
> >>
> >> "Imran Koradia" <nojunk@microsoft.com> wrote in message
> >> news:e351JdEpEHA.2684@TK2MSFTNGP11.phx.gbl...
> >> >
> >> > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in
> > message
> >> > news:ehWTRSEpEHA.1160@tk2msftngp13.phx.gbl...
> >> >> Imran,
> >> >> Why change the DisposableObject parameter to Object?
> >> >>
> >> >> Joop's original function will have a compile error with Option
Strict
> > On,
> >> >> and a runtime error with Option Strict Off & a loosely typed
variable.
> >> >>
> >> >> For example:
> >> >>
> >> >> Option Strict On
> >> >> Dim o As Object
> >> >> DisposeObject(o) ' causes a compile error
> >> >
> >> > Correct. I mentioned in my second post that we're only looking at
> >> > option
> >> > strict off which I should have mentioned in my first post :-S. Joop's
> >> > technique is perfectly alright when option strict is on.
> >> >
> >> >>
> >> >> Option Strict Off
> >> >> Dim o As Object
> >> >> DisposeObject(o) ' causes an InvalidCastException at runtime
> >> >
> >> > Thats the point - its throwing an exception when its not really
needed
> > and
> >> > one that can be avoided, IMHO.
> >> >
> >> >> In fact Joop's version will cause compile time errors with Option
> > Strict
> >> >> Off if its obvious that the parameter does not support IDisposable
> >> >> (for
> >> >> example you pass a non inheritable class or structure).
> >> >>
> >> >> Option Strict Off
> >> >> Dim o As String
> >> >> DisposeObject(o) ' causes a compile error
> >> >>
> >> >> I don't see what changing the parameter to Object & explicitly
> >> >> checking
> >> >> for IDisposable is buying you, as the compiler will do that for you
> > with
> >> >> Joop's original function.
> >> >>
> >> >> In fact your version causes runtime errors with both Option Strict
On
> >> >> &
> >> >> Off, which IMHO is rarely desirable!
> >> >
> >> > Why would the version I sent cause a runtime exception ?!? In fact it
> > will
> >> > never ever throw a runtime exception since its going to dispose off
> >> > only
> >> > those objects that implement the IDisposable interface. So unless an
> >> > exception is thrown from within the dispose, I don't see how that
> > version
> >> > would result in a runtime exception.
> >> >
> >> >
> >> > Imran.
> >> >
> >> >
> >>
> >>
> >
> >
>
>