Writing .NET code using methods


Summary
This topic shows how to write .NET code by reading the graphical interface keys on interface and method help pages in the ArcObjects namespace reference.

In this topic


About writing .NET code using methods

There are several ways to use the available methods on the ArcObjects interfaces. While the graphical representation on the interface help pages look the same (single black arrow), it is the .NET syntax on the individual property help pages that enhance your ArcObjects coding experience.
The following table shows the symbol that denotes methods in the object model diagrams (OMDs) and in the interface:
Interface key
Method symbol
Identifying a method
When you have identified an interface's method to use, click the method's link to obtain the correct .NET syntax. See the following screen shots:
Methods take zero or more input arguments (parameters) and have zero or one return values. The input arguments can be of types that are obtained by value (ByVal, the default) or by reference (ByRef).
 
Return values that result in ArcObjects interfaces are returned by reference. Return values that result in intrinsic objects (that is, the default types from Microsoft, such as System.String, System.Boolean, System.Double, and so on) are returned by value. Return values that result in ArcObjects enumerations (or constants) are returned by value.
  • By value (ByVal) means data is assigned to a value type. The data is stored in the variable on the stack. The stack is a memory area in which processes and threads store data of a fixed size.
  • By reference (ByRef) means data is assigned to a reference type. The data is stored on the managed heap. For reference data types, the address of the data is stored in the stack and the heap holds the data. Data in the heap is dynamic in size. In some programming languages—for example, C++ and Java—this is the same as storing a pointer.

Using the method to write .NET code

The following discusses how to use the method help pages to write .NET code using ArcObjects:
 
Input parameters by value
The following screen shot shows method input parameters by value:
 
See the following code example:
[VB.NET]
Public Class Class1
    
    Public Sub Test()
        
        Dim point As IPoint = New PointClass
        
        'The .PutCoords method input arguments are ByVal, meaning the data values are fixed.
        point.PutCoords(100.0, 100.0)
        
        'You can also specify a variable and set its intial value.
        Dim myX As Double = 50.0
        Dim myY As Double = 50.0
        point.PutCoords(myX, myY)
        
    End Sub
    
End Class
[C#]
public class Class1
{

    public void test()
    {
        IPoint point = new PointClass();

        //The .PutCoords method input arguments are ByVal, meaning the data values are fixed. 
        point.PutCoords(100.0, 100.0);

        //You can also specify a variable and set its intial value.
        double myX = 50.0;
        double myY = 50.0;
        point.PutCoords(myX, myY);
    }

}
Input parameters by reference
The following screen shot shows method input parameters by reference:
See the following code example:
[VB.NET]
Public Class Class1
    
    Public Sub Test()
        
        ' Create a test point.
        Dim point As IPoint = New PointClass
        point.PutCoords(100.0, 100.0)
        
        'Results in X=100 and Y=100.
        System.Windows.Forms.MessageBox.Show("X: " + point.X.ToString + vbCrLf + "Y: " + point.Y.ToString, "Initialize a sample point")
        
        ' Important: When passing in arguments to a ByRef method,
        ' they must be declared first as they will hold the values. It is
        ' good coding practice to instantiate to a default value (Nothing)
        ' in this case.
        Dim refX As System.Double = Nothing
        Dim refY As System.Double = Nothing
        
        'Results in refX=0 and refY=0.
        System.Windows.Forms.MessageBox.Show("refX: " + refX.ToString + vbCrLf + "refY: " + refY.ToString, "Initial values of ByRef")
        
        'Now pass in the variables to the .QueryCoords methods that expect the arguments ByRef
        point.QueryCoords(refX, refY).
        
        'Results in refX=100 and refY=100 because they take on what was stored in memory of the initial point.
        System.Windows.Forms.MessageBox.Show("refX: " + refX.ToString + vbCrLf + "refY: " + refY.ToString, "New values of ByRef")
        
    End Sub
    
End Class
[C#]
public class Class1
{

    public void test()
    {
        // Create a test point.
        IPoint point = new PointClass();
        point.PutCoords(100.0, 100.0);

        //Results in X=100 and Y=100
        System.Windows.Forms.MessageBox.Show("X: " + point.X.ToString() +
            System.Environment.NewLine + "Y: " + point.Y.ToString(), 
            "Initialize a sample point");

        // Important: When passing in arguments to a ByRef method,
        // they must be declared first as they will hold the values. It is  
        // good coding practice to instantiate to a default value (0)
        // in this case.
        System.Double refX = 0;
        System.Double refY = 0;

        //Results in refX=0 and refY=0.
        System.Windows.Forms.MessageBox.Show("refX: " + refX.ToString() +
            System.Environment.NewLine + "refY: " + refY.ToString(), 
            "Initial values of ByRef");

        //Now pass in the variables to the .QueryCoords methods that expect the arguments ByRef
        point.QueryCoords(out refX, out refY);

        //Results in refX=100 and refY=100 because they take on what was stored in memory of the initial point.
        System.Windows.Forms.MessageBox.Show("refX: " + refX.ToString() +
            System.Environment.NewLine + "refY: " + refY.ToString(), 
            "New values of ByRef");
    }

}
Input with a return parameter
The following screen shot shows a methods with a return parameter:
See the following code example:
[VB.NET]
Public Class Class1
    
    Public Sub Test()
        
        ' Create a test point.
        Dim pointA As IPoint = New PointClass
        pointA.PutCoords(100.0, 100.0)
        
        ' Create another test point.
        Dim pointB As IPoint = New PointClass
        pointB.PutCoords(50.0, 50.0)
        
        ' The .Compare method has a return value.
        ' System.Int32 is an int in .NET
        Dim intReturn As System.Int32 = pointA.Compare(pointB)
        
    End Sub
    
End Class
[C#]
public class Class1
{

    public void test()
    {
        // Create a test point.
        IPoint pointA = new PointClass();
        pointA.PutCoords(100.0, 100.0);

        // Create another test point.
        IPoint pointB = new PointClass();
        pointB.PutCoords(50.0, 50.0);

        // The .Compare method has a return value.
        //System.Int32 is an int in .NET
        System.Int32 intReturn = pointA.Compare(pointB);
    }

}


See Also:

Understanding the ArcObjects namespace reference




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):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine