This is exactly the reason that TryCast() was introduced.
I take it from your numbers that you only exceuted 1 iteration of the loop.
What happens if you execute 1 million or more iterations?
Seeing as Ticks are 'too fast for your old brain' to deal with, convert the
result up to a more meaningful unit. 99.9 recurring % of humans cannot
visualize 100 nanoseconds, but we can quite easily visualize seconds and
even milliseconds and microseconds.
Try this variation on your test and you will see the 'power' of TryCast().
Private Interface Tester
End Interface
Private Class WithNoInterface
End Class
Private Class WithInterface
Implements Tester
End Class
Private Sub WithOutUseTryCa tch()
Dim inter As Tester = Nothing
Dim obj As New WithNoInterface
Dim _st As Stopwatch = Stopwatch.Start New
For i As Integer = 1 To 1000000
inter = TryCast(obj, Tester)
Next
Console.WriteLi ne("{0:#0.00000 00000}", _st.ElapsedTick s /
Stopwatch.Frequ ency)
Console.WriteLi ne(inter Is Nothing)
Console.WriteLi ne()
End Sub
Private Sub WithOutUseRefle ction()
Dim inter As Tester = Nothing
Dim obj As New WithNoInterface
Dim _st As Stopwatch = Stopwatch.Start New
For i As Integer = 1 To 1000000
If obj.GetType().G etInterface("Te ster") IsNot Nothing Then inter =
CType(obj, Tester)
Next
Console.WriteLi ne("{0:#0.00000 00000}", _st.ElapsedTick s /
Stopwatch.Frequ ency)
Console.WriteLi ne(inter Is Nothing)
Console.WriteLi ne()
End Sub
Private Sub WithUseTryCatch ()
Dim inter As Tester = Nothing
Dim obj As New WithInterface
Dim _st As Stopwatch = Stopwatch.Start New
For i As Integer = 1 To 1000000
inter = TryCast(obj, Tester)
Next
Console.WriteLi ne("{0:#0.00000 00000}", _st.ElapsedTick s /
Stopwatch.Frequ ency)
Console.WriteLi ne(inter Is Nothing)
Console.WriteLi ne()
End Sub
Private Sub WithUseReflecti on()
Dim inter As Tester = Nothing
Dim obj As New WithInterface
Dim _st As Stopwatch = Stopwatch.Start New
For i As Integer = 1 To 1000000
If obj.GetType().G etInterface("Te ster") IsNot Nothing Then inter =
CType(obj, Tester)
Next
Console.WriteLi ne("{0:#0.00000 00000}", _st.ElapsedTick s /
Stopwatch.Frequ ency)
Console.WriteLi ne(inter Is Nothing)
Console.WriteLi ne()
End Sub
"Lloyd Sheen" <a@b.cwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I noticed a thread a few days ago about the use of Try.. Catch versus
testing variables etc and had in my mind to test exactly what the impact
was. Its a mind boggler.
I created a small windows app that executed two subroutines.
The first simply created an object which does not implement an interface
and attempts to assign that object to an variable of the interface type.
This is done in a try ... catch block.
Private Sub UseTryCatch()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Try
inter = obj
Catch et As InvalidCastExce ption
Dim iii As Integer = 1
Catch ex As Exception
Dim ii As Integer = 1
End Try
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeCatch.Te xt = interv.ToString
End Sub
The second creates the same object but uses reflection to test if the
object supports the interface and if so then the assignment would take
place.
Private Sub UseReflection()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Dim typ As Type = obj.GetType
If typ.GetInterfac e("Tester") IsNot Nothing Then
inter = obj
End If
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeReflecti on.Text = interv.ToString
End Sub
The results of this test were that the try .. catch took 101087280 ticks
(about 5 seconds on a dual 3.0 G).
The reflection test took 9765 ticks (too fast to determine in my old
brain).
The testing before trying was 10352 times faster. I would think that
would put to rest any questions as to the best method of capturing errors.
Look before you leap.
Hope this helps
Lloyd Sheen