Creating objects using non-creatable classes


Summary Finding the right interface to access non-creatable classes can be a challenge. This topic shows what a non-creatable object is and the ways in which it can be accessed.

In this topic


About creating objects using non-creatable classes

There are cases where you want to use an ArcObject, but it is specified as a non-creatable object in the ArcObjects namespace reference. A non-creatable object cannot be created using the "New" keyword. A reference to the non-creatable object must be obtained through other objects (that is, it must be created from the result of a property or method on a different object). This is a harder to determine than declaring an object as new (see Creating variables using the New keyword for details).
Adding an object
Consider the scenario to add a MapInsetWindowClass object to a map. First, examine the help document for the MapInsetWindowClass Class. See the following screen shot:
If you had not seen the portion of the document stating that the MapInsetWindow is a non-creatable object, you may have thought that you could create a MapInsetWindowClass object using the following code example, which results in a compile time error:
[VB.NET]
Public Class Class1
    
    Public Sub Test()
        
        ' The MapInsetWindowClass is non-creatable and causes a compile time error in
        ' Visual Studio similar to the following:
        ' ESRI.ArcGIS.ArcMapUI.MapInsetWindowClass.Private Sub New()' is not accessable
        ' in this context because it is 'Private'.
        Dim mapInsetWindow As IMapInsetWindow = New MapInsetWindowClass
        
    End Sub
    
End Class
[C#]
public class Class1
{
    public void test()
    {
        // The MapInsetWindowClass is non-creatable and causes a compile time error in 
        // Visual Studio similar to the following:
        // The type 'ESRI.ArcGIS.ArcMapUI.MapInsetWindowClass' has no constructors defined. 
        IMapInsetWindow mapInsetWindow = new MapInsetWindowClass();
    }
}
Using non-creatable ArcObjects
As previously mentioned, the MapInsetWindow object cannot be instantiated with the "New" keyword and if you try to compile this code, it will not work. Our goal will be to use some other ArcObject and obtain an interface that is supported by the MapInsetWindowClass. The following steps are recommended to use non-creatable ArcObjects:
  1. Locate the interfaces implemented by the specified class (MapInsetWindowClass) that do not end with "events" in the ArcObjects namespace reference help. In this example, the interfaces are IDataWindow, IDataWindow2, ILensWindow, and IMapInsetWindow (see the previous screen shot).
  2. Search the help using each of the interface names selected in Step 1 as keywords. Look for help documents that return one or more of these interfaces as a property or method from another object.
    • The key to finding the help document or documents you want from the returned results are those in which the title does not contain Interface in the name and does end with the words Property or Method. These documents will most likely return the desired interface. See the following screen shot:

The following table summarizes the search results using the keywords listed in Step 1. The document listed in the second column meets the desired criteria.
Keyword
Help document title
IDataWindow
IDataWindowFactory.Create Method
IDataWindow2
None
ILensWindow
None
IMapInsetWindow
None
Since the IDataWindowFactory.Create Method is the only document that met the search criteria, we will investigate it's help page to see if this help's us solve our ultimate goal of obtaining an interface on MapInsetWindowClass object to program against. The following screen shot shows the IDataWindowFactory.Create Method help document:
In the preceding screen shot, under the See Also area, click IDataWindowFactory Interface and the following help document appears:
 
In the preceding screen shot, "Classes that implement IDataWindowFactory" indicates you can create an object from these choices. Now you can create an object and utilize one of its methods to obtain the interface for the non-creatable MapInsetWindow object. For more information, see Creating variables using the New keyword.
The following code example shows how the code can look when using the functionality on MapInsetWindowClass:
[VB.NET]
Public Class Class1
    
    Public Sub Test(ByVal application As IApplication)
        
        Dim dataWindowFactory As IDataWindowFactory = New MapInsetWindowFactoryClass
        
        ' IDataWindow is an interface on the non-creatable MapInsetWindowClass.
        Dim dataWindow As IDataWindow = dataWindowFactory.Create(application)
        
        ' TypeOf determines if dataWindow interface implements the MapInsetWindowClass.
        If TypeOf dataWindow Is MapInsetWindow Then
            
            ' Cast to different interfaces on the MapInsetWindowClass.
            Dim dataWindow2 As IDataWindow2 = CType(dataWindow, IDataWindow2)
            
        End If
        
    End Sub
    
End Class
[C#]
public class Class1
{

    public void test(IApplication application)
    {
        IDataWindowFactory dataWindowFactory = new MapInsetWindowFactoryClass();

        // IDataWindow is an interface on the non-creatable MapInsetWindowClass.
        IDataWindow dataWindow = dataWindowFactory.Create(application);

        // TypeOf determines if dataWindow interface implements the MapInsetWindowClass.

        if (dataWindow is MapInsetWindow)
        {
            // Cast to different interfaces on the MapInsetWindowClass.
            IDataWindow2 dataWindow2 = (IDataWindow2)dataWindow;
        }
    }

}


See Also:

Understanding the ArcObjects namespace reference
Creating variables using the New keyword




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
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime