Select to view content in your preferred language

how to get the value of IField ????

4007
13
07-09-2010 06:39 AM
AbdulrahmanAlqasim
Emerging Contributor
i want to get the field value of a feature class using this code:

pFeature = pFeatureCursor.NextFeature();
pField = pFeature.Fields.get_Field(0);

how i can get the value of pField.
thank you
0 Kudos
13 Replies
pavankalvala
Emerging Contributor
Here is the full code: (upon selecting a feature, it should display field name and field values)

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal Y As Integer)
       
        Dim pMxdoc As IMxDocument
        Dim pActiveView As IActiveView
        Dim pEnumLayer As IEnumLayer
        Dim pLayer As ILayer
        Dim pFlayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pSelectionSet As ISelectionSet
        Dim pEnumFeat As IEnumFeature
        Dim pFeat As IFeature
        Dim MC As Double
        Dim pFeatSel As IFeatureSelection
        Dim pMap As IMap
        Dim pfeatureclass As IFeatureClass
        Dim fields As IFields
        Dim C As Long
        Dim ii As Long
        Dim Field As IField
        Dim intFldIndex As Integer
        Dim fieldname As String
        Dim fieldvalue As Integer
        Dim value As Long              // should it be different (string/integer/double or what?)
        Dim a As Integer
        Dim namelist As String


        pMxdoc = m_application.Document
        pActiveView = pMxdoc.FocusMap
        pMap = pMxdoc.FocusMap
        pFlayer = pMxdoc.SelectedLayer
        pfeatureclass = pFlayer.FeatureClass
        fields = pfeatureclass.Fields
        pFeatureSelection = pFlayer
        pSelectionSet = pFeatureSelection.SelectionSet

        ' verify the name of the selected layer
        Dim count As Integer
        count = pSelectionSet.Count
        If pSelectionSet.Count <> 0 Then
            MsgBox("You have have selected:   " & pFlayer.Name)
        End If

        ' get the field count
        C = fields.FieldCount
        MsgBox("There are:   " & C & "fields in this layer")

        pEnumFeat = pMxdoc.FocusMap.FeatureSelection
        pFeat = pEnumFeat.Next

        ' loop through each field and add the field name to a list
        For ii = 0 To fields.FieldCount - 1
            Field = fields.Field(ii)
            fieldname = Field.Name
            fieldvalue = Field.VarType
            value = pFeat.Value(ii)               // having trouble in getting the value
            namelist = namelist & fieldname & value & Chr(13)
        Next

        ' diaplay the list of field names in a messagebox
        MsgBox(namelist, , "field names")

        ' loop to get the field value
        'For a = 0 To pFeat.Fields.FieldCount - 1
        '    value = pFeat.Value(a)
        '    namelist = namelist & value & Chr(13)
        'Next

        'MsgBox(namelist, , "field value")

         
    End Sub
0 Kudos
AlexanderGray
Honored Contributor
Yes the value could be any type.  In fact different fields will have different types.  The IField.Type will get you the field type and allow you do what you need to with the value.  If you need to deal with all the values in the same way, string is probably the most flexible type you can use.  However casting a shape or a blob to a string can yield unpredictable results, you need to check the field type and handle those (I usually just use the word "Shape" or "Blob" or "raster" if I need a string.)  Dates also need to be converted to string with the pattern of your choice.

Further mode. if any of your rows have null values in them, the code won't work, you need to declare the value variable as object and check if the it is dbnull.value (in .net), trying to cast dbnull value to string causes an exception.
0 Kudos
pavankalvala
Emerging Contributor
I have code below to pull field value from a feature selction onto a word document template. Instead of hard coding the field names, I would like to go through config file. How can this be done.

Here is the code below:

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
      
        Dim pMxdoc As IMxDocument
        Dim pActiveView As IActiveView
        Dim pEnumLayer As IEnumLayer
        Dim pLayer As ILayer
        Dim pFlayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pSelectionSet As ISelectionSet
        Dim pEnumFeat As IEnumFeature
        Dim pEnumFeatSet As IEnumFeatureSetup
        Dim pFeat As IFeature
        Dim A As Double
        Dim B As String
        Dim B1 As String
      
        Dim pFeatSel As IFeatureSelection
        Dim pMap As IMap
        Dim pfeatureclass As IFeatureClass
        Dim fields As IFields
        Dim C As Long
        Dim ii As Long
        Dim Field As IField
        Dim intFldIndex As Integer
        Dim fieldname As String
        Dim fieldvalue As Integer
        Dim value As String
        Dim namelist As String
        Dim wdApp As Microsoft.Office.Interop.Word.Application
        Dim mydoc As Microsoft.Office.Interop.Word.Document
        Dim mywdRange As Microsoft.Office.Interop.Word.Range


        pMxdoc = m_application.Document
        pActiveView = pMxdoc.FocusMap
        pMap = pMxdoc.FocusMap
        pFlayer = pMxdoc.SelectedLayer
        pfeatureclass = pFlayer.FeatureClass
        fields = pfeatureclass.Fields
        pFeatureSelection = pFlayer
        pSelectionSet = pFeatureSelection.SelectionSet


       
        ' Verify that there is at least 1 layer in the TOC 
        If pMap.LayerCount < 1 Then
            MsgBox("Must have at least 1 layer in your map.")
            Exit Sub
        End If

        ' verify the name of the selected layer
        Dim count As Integer
        count = pSelectionSet.Count
        If pSelectionSet.Count <> 0 Then
            MsgBox("You have have selected:   " & pFlayer.Name)
        End If

        ' get the field count
        C = fields.FieldCount
        MsgBox("There are:   " & C & "fields in this layer")

        pEnumFeat = pMxdoc.FocusMap.FeatureSelection
        'pFeat = pEnumFeat.Next

        pEnumFeatSet = pEnumFeat
        pEnumFeatSet.AllFields = True
        pFeat = pEnumFeat.Next

        ' loop through each field and add the field name to a list
        For ii = 0 To fields.FieldCount - 1
            Field = fields.Field(ii)
            fieldname = Field.Name                  ' this gives the field name
            value = pFeat.Value(ii).ToString      ' this gives the field value
            namelist = namelist & fieldname & value & Chr(13)
        Next

        ' diaplay the list of field names in a messagebox
        MsgBox(namelist, , "field names and value")

    
        ' loop through to get value of FID
        Do While (Not pFeat Is Nothing)
  A = pFeat.Value(pFeat.Fields.FindField("UFO_ID"))    // @@@@ want to get this through config file
  B = pFeat.Value(pFeat.Fields.FindField("DATECREATE"))
          
            pFeat = pEnumFeat.Next
            'MsgBox(A)
        Loop

        ' opening word doc with the field values inside the word bookmarks
        wdApp = New Microsoft.Office.Interop.Word.Application
        With wdApp
            .Visible = True
            .WindowState = WdWindowState.wdWindowStateMaximize
        End With

        mydoc = wdApp.Documents.Add(Template:="C:\Documents and Settings\ZQ3079\Mobile project\ExportWord(VB)\Audit Form Final.doc")
        With mydoc.Bookmarks
            .Item("A").Range.InsertAfter(A)   ' "A" is bookmark name in word document
            .Item("C").Range.InsertAfter(B)   ' "C" is bookmark name in word document
     
        End With
    End Sub

Let me know.
0 Kudos
pavankalvala
Emerging Contributor
I am populating field values from selected features onto a word document bookmark. I need code help in assigning the value to the bookmark range in c#.

Here is the code I am currently working with C#.

// opening word application
wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.Visible = true;
wdApp.WindowState = WdWindowState.wdWindowStateMaximize;

// opening the word document from config file
wddoc = wdApp.Documents.Add(ConfigurationSettings.AppSettings["WordDocumentPath"]);
           
{
   var with = wddoc.Bookmarks;
                                          
with.item("A").Range.InsertAfter(A);    // @@@ help here:
                         }
        }

with.item is not acceptable in the c#. getting error at . item. What do I write In order to make it work in C#. Please help.
0 Kudos