This document is archived and information here might be outdated.  Recommended version.


How to get returned messages (ArcObjects .NET 10.5 SDK)

How to get returned messages


Getting returned messages

When tools are executed in a program, you can access resulting error or warning messages or simply show the progress of an operation. The returned messages can be filtered to only those with a certain severity, using the GetMessages method with the severity option. 
The following code example shows how to print only the error messages:
[C#]
public void ExamplePrintGPErrorMessages(Geoprocessor gp)
{

    object sev=2;
    string messages=gp.GetMessages(ref sev);
    System.Console.WriteLine(messages);

}
[VB.NET]
Public Sub ExamplePrintGPErrorMessages(ByVal gp As Geoprocessor)
    
    Dim sev As Object=2
    Dim messages As String=gp.GetMessages(sev)
    System.Console.WriteLine(messages)
    
End Sub
Individual messages can be retrieved using the GetMessage method. This method has one parameter, which is the index of the message in the geoprocessor's message list or array. The MessageCount property maintains the number of messages in the geoprocessor's message array. The following code example returns all the tool's messages:
[C#]
public void ExamplePrintAllGPMessages(Geoprocessor gp, Union uniontool)
{

    // Execute the Union tool.
    gp.Execute(uniontool, null);

    if (gp.MessageCount > 0)
    {

        for (int Count=0; Count <= gp.MessageCount - 1; Count++)
        {

            Console.WriteLine(gp.GetMessage(Count));

        }
    }
}
[VB.NET]
Public Sub ExamplePrintAllGPMessages(ByVal gp As Geoprocessor, ByVal uniontool As Union)
    
    ' Execute the Union tool.
    gp.Execute(uniontool, Nothing)
    
    If gp.MessageCount > 0 Then
        
        For Count As Integer=0 To gp.MessageCount - 1
            
            Console.WriteLine(gp.GetMessage(Count))
            
        Next Count
        
    End If
    
End Sub
As shown in the previous code example, messages are retrieved using methods available on the geoprocessor. However, it is also possible to get the messages from the IGeoProcessorResult2 interface. Thus, when executing geoprocessing server tools, you need to retrieve the messages from IGeoProcessorResult. See the following code example:
[C#]
public void ExamplePrintAllGPMessages2(Geoprocessor gp, Union uniontool)
{

    // Execute the Union tool.
    IGeoProcessorResult2 pResult=(IGeoProcessorResult2)gp.Execute(uniontool, null);

    if (pResult.MessageCount > 0)
    {

        for (int Count=0; Count <= pResult.MessageCount - 1; Count++)
        {

            Console.WriteLine(pResult.GetMessage(Count));

        }

    }

}
[VB.NET]
Public Sub ExamplePrintAllGPMessages2(ByVal gp As Geoprocessor, ByVal uniontool As Union)
    
    ' Execute the Union tool.
    Dim pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2=CType(gp.Execute(uniontool, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
    
    If pResult.MessageCount > 0 Then
        
        For Count As Integer=0 To pResult.MessageCount - 1
            
            Console.WriteLine(pResult.GetMessage(Count))
            
        Next Count
        
    End If
    
End Sub


See Also:

Working with result objects




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):