calculate field fails

3506
4
Jump to solution
12-24-2014 09:29 AM
Highlighted
Occasional Contributor II

I have an IFeatureClass created in my code and I have added a new field to it. I'm stuck on calculating values for the new field. I've tried using CalculateField and ICalculator but I get errors from both (see code below). Any suggestions?

IFeatureClass classifiedPolygons_IFC

IFieldEdit2 field

    

/*

* Error HRESULT E_FAIL has been returned from a call to a COM component.

* I've also tried casting the IFeatureClass to ITable

*/

CalculateField calculateField = new CalculateField()

{

    in_table = classifiedPolygons_IFC,

    field = classifiedPolygons_IFC.Fields.Field[classifiedPolygons_IFC.Fields.FindField(field.Name)],

    expression = "Calc Val",

    expression_type = "Python"

};

Geoprocessor gp = new Geoprocessor();

gp.Execute(calculateField, null);

                

    

/*

* "Retrieving the COM class factory for component with CLSID {D676066E-38CA-429A-B846-DA7A8446C52D} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."

*/

ICursor updateCursor = classifiedPolygons_IFC.Update(null, false) as ICursor;// table.Update(null, false);

ICalculator calculator = new Calculator()

{

    Cursor = updateCursor,

    Field = field.Name,

    Expression = "Calc Val"

};

calculator.Calculate();

Marshal.FinalReleaseComObject(updateCursor);

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Honored Contributor

Calc Val is a string and expression is a string variable, but the string must be quoted twice, once for the actual value in the field calucator, and another time for the string variable.  So expression = "Calc Val" won't work, but expression = "'Calc Val'" should work (single quotes inside double quotes work for Python calculations, but not VB Script calculations.  For VB Script it would be expression = """Calc Val""").

View solution in original post

Reply
0 Kudos
4 Replies
Highlighted
Regular Contributor II

Hi

If
you open an update cursor you should just calculate the values.

Check
the update cursor example (and a few other examples) here: http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/0001/0001000001mm000000.htm

Have
fun

Mody

Reply
0 Kudos
Highlighted
MVP Honored Contributor

Calc Val is a string and expression is a string variable, but the string must be quoted twice, once for the actual value in the field calucator, and another time for the string variable.  So expression = "Calc Val" won't work, but expression = "'Calc Val'" should work (single quotes inside double quotes work for Python calculations, but not VB Script calculations.  For VB Script it would be expression = """Calc Val""").

View solution in original post

Reply
0 Kudos
Highlighted
Occasional Contributor II

I'm on holiday right now but I'll give it a try on Monday and let you know how it goes. Cheers.

Reply
0 Kudos
Highlighted
Occasional Contributor II

Thanks for catching that. I'm sure I'll be posting more questions as I work through creating my first SOE.

Reply
0 Kudos