Interfaces from the same coclass, why some need explicit QI, some don't?

1039
6
06-19-2011 02:42 PM
weiliang
New Contributor II
Hi,

I try to create a new map document in C# with the following code and cast it to an interface, while casting to IDocument I can implicitly cast to: IDocument iDoc= new MxDocument(); however, I need explicitly cast for the following interface: IMxDocument IMxDoc = new MxDocument() as IMxDocument. Is there any particular reason that one interface can implicit cast but the other can't?

Also, from the attached OMD, how can I tell that IDocument is defined in Framework assembly rather than ArcMapUI assembly, is this a bug for this OMD?

Thanks for your input.

Wei
0 Kudos
6 Replies
AlexanderGray
Occasional Contributor III
If memory serves me IDocument is the default interface for the mxdocumentclass.  The cast is not necessary.

http://edndoc.esri.com/arcobjects/9.1/ExtendingArcObjects/Ch02/CodingInterfaces.htm

The IDocument is in the framework namespace and the IMxDocument is in the ArcMapUI namespace.  IDocument is a generic interface implemented by other types of document such as a ArcGlobe document (GMxDocument), ArcCatalog document (GxDocument), ArcScene (SxDocument)  so it has to be a generic namespace implemented by all these applications.  IMxDocument interface has methods and properties specific to the an ArcMap Document (*.mxd) it is only implemented by an MxDocumentClass in ArcMap.
0 Kudos
weiliang
New Contributor II
Many thanks for your help, Alexander.

However, from the OMD (or other places), how can you tell IDocument is the default interface rather than IMxDocument?

Also, from the attached OMD, how can I tell IDocument is defined in Framework assembly (I thought there should be a prefix in this interface name should it defined in other assembly. Example: esriCarto.IMapDocument ). 

Besides, is there a way you can tell one interface is generic while the other isn't?

Wei
0 Kudos
RichardWatson
Frequent Contributor
You can find which library a given interface or class is in by searching the help.  For example:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//000v000001p2000000

The underlying machine here is COM which defines the metadata using a type library.  On my machine, the ESRI type libraries are in C:\Program Files (x86)\ArcGIS\com.  If you use the OleView utility (which comes with Visual Studio - mine is C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\OleView.Exe) you can view the contents of the library.  When I open esriArcMapUI.olb and search for MxDocument this is what I found:

    [
      uuid(006B1AFE-C66C-11D0-B94C-080009EE4E51),
      helpstring("ESRI Mx Document."),
      helpcontext(0x00002723)
    ]
    coclass MxDocument {
        [default] interface IDocument;
        interface IMxDocument;
        interface IDocumentDirty;
        interface IDocumentDirty2;
        interface IPropertySupport;
        interface IChangeLayout;
        interface IDocumentDefaultSymbols;
        interface IContentsViewEdit;
        interface IDataGraphs;
        interface IDataGraphCollection;
        interface IDocumentDatasets;
        interface IDocumentInfo;
        interface IReportUnitFormat;
        interface IReportUnitFormat2;
        interface IDocumentVersion;
        interface IPersist;
        [default, source] interface IDocumentEventsDisp;
        [source] interface IDocumentEvents;
        [source] interface IDataGraphCollectionEvents;
    };

An interface has no notion of generic.  In the discussion below the word "generic" really means that multiple classes implement a given interface.  Your best insight here comes from reading the documentation for the particular interface.
0 Kudos
weiliang
New Contributor II
Many thanks for your kindly help, Richard.

Wei
0 Kudos
AlexanderGray
Occasional Contributor III
I am not sure how to tell which is the default interface, I have never really thought of that.  I guess I blindly cast when the compiler complains. 
I don't use the OMDs much.  I remember looking at them 7-8 years ago in confusion.  I know people who swear by them but I am not a visual person and I find there are far too many objects and interfaces and links between OMDs and assemblies, the OMDs are just not for me.  I use the help files installed with the dev kit or the online ones.  If that fails I look in this forum or on google, the OMD is pretty much the last place I look.
0 Kudos
weiliang
New Contributor II
Many thanks for your detailed explain and helpful recommendations, Alexander.

Have a great afternoon!

Wei
0 Kudos