I have made an attempt to create an ArcObjects addin to help with some workflows here in the office. I apologize in advance if I have something fundamentally wrong with the code. This is my first ArcObjects endeavor. Essentially I have geodatabases with a large number of feature classes. Each feature class has the fields "DateModified" and "ModifiedBy" and I am hoping to populate them as edits are made. I am aware of editor tracking currently in ArcGIS, but the powers that be would like to track edits without changing current data models.
It appears that the code below crashes ArcMap when it attempts to store the feature.
So my questions are 1) Can someone tell me why my code causes ArcMap to crash? 2) Are there easier ways to accomplish what I have been tasked with? Thanks in advance.
I have the following code:
Public Class TrackEditsExtension
Inherits ESRI.ArcGIS.Desktop.AddIns.Extension
Public Sub New()
End Sub
'Invoked when the Editor Extension is loaded
Protected Overrides Sub OnStartup()
AddHandler Events.OnStartEditing, AddressOf Events_OnStartEditing
AddHandler Events.OnStopEditing, AddressOf Events_OnStopEditing
End Sub
'Invoked at the start of the Editor Session
Private Sub Events_OnStartEditing()
AddHandler Events.OnCreateFeature, AddressOf Events_OnCreateChangeFeature
AddHandler Events.OnChangeFeature, AddressOf Events_OnCreateChangeFeature
System.Windows.Forms.MessageBox.Show("Editor tracking extension enabled.")
End Sub
'Invoked at the end of the Edit session
Private Sub Events_OnStopEditing()
RemoveHandler Events.OnCreateFeature, AddressOf Events_OnCreateChangeFeature
RemoveHandler Events.OnChangeFeature, AddressOf Events_OnCreateChangeFeature
End Sub
'Invoked when a feature is created or modified
Private Sub Events_OnCreateChangeFeature(ByVal obj As ESRI.ArcGIS.Geodatabase.IObject)
Dim feature As IFeature = CType(obj, IFeature)
Dim dateIndex As Integer = feature.Fields.FindField("DateModified")
Dim nameIndex As Integer = feature.Fields.FindField("ModifiedBy")
Dim changeMade As Boolean = False
If dateIndex > 0 Then
feature.Value(dateIndex) = DateTime.Now
changeMade = True
End If
If nameIndex > 0 Then
feature.Value(nameIndex) = Environ("USERNAME")
changeMade = True
End If
If changeMade = True Then
feature.Store()
End If
End Sub
Protected Overrides Sub OnShutdown()
End Sub
End Class
Store should not be called inside of edit events, such as OnCreateFeature, OnChangeFeature or OnDeleteFeature. Even if you are modifying the geometry or other field values, Store will be called once the event is complete.