IEditTemplate SetDefault Value help

2016
2
10-02-2015 11:44 AM
StacyMcNeil
New Contributor II

I am trying to set a default value for a field. I have created a script in VB.Net that will access the layers I am interested in and get the edit template for each one.  I can access all of the properties in the IEditTemplate interface but when I try and access the SetDefaultValue method I get an error - Error HRESULT E_FAIL has been returned from a call to a COM component.  I have 3 message boxes that display the values without error but as soon as it reaches the SetDefaultValue it errors out.  Any help would be greatly appreciated!

                    Dim idx As Integer
                    Dim pLayer As ILayer
                    Dim pFeatureLayer As IFeatureLayer2
                    Dim pFeatureClass As IFeatureClass
                    Dim pDataset As IDataset
                    Dim pEnumLayer As IEnumLayer


                    pEnumLayer = pMap.Layers(Nothing, True)
                    pEnumLayer.Reset()
                    pLayer = pEnumLayer.Next


                    For idx = 0 To pMap.LayerCount - 1
                        Do Until pLayer Is Nothing
                            If TypeOf pLayer Is IFeatureLayer2 And Not TypeOf pLayer Is RasterLayer Then
                                pFeatureLayer = pLayer
                                pFeatureClass = pFeatureLayer.FeatureClass
                                If pFeatureLayer.DataSourceType = "SDE Feature Class" Then
                                    If Not pFeatureClass.FeatureDataset Is Nothing Then
                                        pDataset = pFeatureClass.FeatureDataset
                                        Dim pLayerExt As ILayerExtensions
                                        pLayerExt = pLayer
                                        Dim pEditTemplateMgr As IEditTemplateManager
                                        Dim jdx As Integer
                                        For jdx = 0 To pLayerExt.ExtensionCount - 1
                                            Dim extension As Object = pLayerExt.Extension(jdx)
                                            If TypeOf extension Is IEditTemplateManager Then
                                                pEditTemplateMgr = extension
                                                Dim pEditTemplate As IEditTemplate = pEditTemplateMgr.EditTemplate(0)
                                                MsgBox(pLayerExt.ExtensionCount)
                                                MsgBox(pEditTemplate.Name)
                                                MsgBox(pEditTemplate.DefaultValue("Feature_Type"))
                                                pEditTemplate.SetDefaultValue("Feature_Type", "Lake", True)
                                            End If
                                        Next
                                    End If
                                End If
                            End If
                            pLayer = pEnumLayer.Next
                        Loop
                    Next
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor

According to the help on this method for this Interface is says:

An exception will be raised if the supplied value is not valid for the given field type e.g. If you try to set a text value for an integer field.

You are setting the value "Lake" as the default value, is this correct? You also set the boolean parameter initdefaultvalues to True and in the help it points out that a subtype can be integer or text.  I would look there first and double check your field types.

0 Kudos
StacyMcNeil
New Contributor II

Thank you Duncan.  Yes I am setting "Lake" as the default value.  The problem is that I can access all of the properties on the Edit Template Interface but it seems that the methods cannot be accessed as shown on the object diagram.  I can get my tool to run as an add-in but have to start an editing session and then the tool will run without error.    My original project however is a standalone form that does not access the ArcMap application, I use the Map Document interface, so it will greatly complicate things if I have to go through the editor in my script.  I have been working a bit with ESRI on this and they will be letting me know if this is an issue since the documentation states you can update default values through the template manager.  Any insights you have on setting the default value without the editor would be great!

0 Kudos