Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

@_dscholtes_  I use Try-Catch beacuse this has better performance in usual way and this is well known construct. When you use .Type check, VB.NET (iLogic) compiler creates late binding construct in background and this has worse performance. See https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-... for more information.

The best solution is use of TryCast. This has the same performance in usual way as Try-Catch and much better in unusual way.

 

And also I use C# fo common work and there is not possible to use .Type check directly.

 

Bellow  is benchmark code sample. Try run this code in part sketch environment and in model environment.

 

Expected results

In PlanarSketch [ms]

DEBUG|Try-Catch: 0,9974
DEBUG|Late binding: 25,932
DEBUG|TryCast: 0,9942

 

In Model [ms]

DEBUG|Try-Catch: 370,0105
DEBUG|Late binding: 19,9461
DEBUG|TryCast: 3,9891

 

 

Dim oSketch As PlanarSketch
Dim start As DateTime
Dim span As TimeSpan
Dim count = 10000
Dim activeEditObject = ThisApplication.ActiveEditObject

'Try-Catch
start = DateTime.Now
For i = 1 To count
	Try
		oSketch = activeEditObject
	Catch
		Continue For
	End Try
Next
span = DateTime.Now - start
Logger.Debug("Try-Catch: " & span.TotalMilliseconds)

'Late binding
start = DateTime.Now
For i = 1 To count
	If (activeEditObject.Type = ObjectTypeEnum.kPlanarSketchesObject) Then
		oSketch = ThisApplication.ActiveEditObject
	Else
		Continue For
	End If
Next
span = DateTime.Now - start
Logger.Debug("Late binding: " & span.TotalMilliseconds)

'TryCast
start = DateTime.Now
For i = 1 To count
	oSketch = TryCast(activeEditObject, PlanarSketch)
	If oSketch Is Nothing Then Continue For
Next
span = DateTime.Now - start
Logger.Debug("TryCast: " & span.TotalMilliseconds)