IWorkspaceFactoryStatus Example

2309
3
10-25-2015 04:00 PM
MarkMindlin
Occasional Contributor III

Hi there,

I need an explanation about the interface IWorkspaceFactoryStatus.

I have an old project that I investigate an error (I described in another discussion).

But what is the difference

1. (in my old project):

IWorkspaceFactoryStatus pWorkspaceFactoryStatus = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();

2. ESRI example

IWorkspaceFactoryStatus workspaceFactoryStatus = (IWorkspaceFactoryStatus)workspaceToCheck.WorkspaceFactory;

I see that example here (see the link)

IWorkspaceFactoryStatus Example

I know about Activator and singleton, I need an explanation WHY the number one (1. (in real project)) works.

10.3, Desktop, .Net 3.5

Any help would be highly appreciated.

0 Kudos
3 Replies
nicogis
MVP Frequent Contributor

in section "Singletons and System.__COMObject" http://help.arcgis.com/en/sdk/10.0/Arcobjects_net/conceptualhelp/index.html#/Interacting_with_single... there is the detail why use Activator class instead of the new keyword to instantiate singletons

FreddieGibson
Occasional Contributor III

I used to have an example that showed proof of this error a couple of years ago, but I haven't been to recreate it so far today. The answer to your question will probably be best described on the page mentioned by Domenico. You'll want to note that the page says "Sometimes when you use the new keyword to instantiate a COM object, you are getting a referent to an object that exists...If your code has encountered this error situation, you might receive an error, such as Unable to cast objecvt of type System.__ComObject to type <Typename>."

The issue is more about a best practice. For a majority of people they'll continue to use the new keyword and they'll never run into this problem. The best practice to avoid this error if you were to ever run into it would be to use the Activator, but it's really up to the programmer to determine how they want to implement workflows related to this.

For me, I ran into this issue when I started learning ArcObjects about six years ago, shortly after reading that page I just modified my workflows to use the Activator where needed and I haven't had to worry about this issue since then. Nowadays I use the Activator so much that I tend to forget that you can even use the new keyword.

0 Kudos
MarkMindlin
Occasional Contributor III

Thanks, Domenico and Freddie,

I see your point clearly. I will definitely use Activator.

What I do not understand in my old project (in comparison with ESRI example) -
How can I obtain all currently open workspaces  "from air"?

How that "magic" happens?

See the code from my project here

Reconnect to SDE by IWorkspaceFactoryStatus causes errors

See the ESRI help

ArcObjects 10 .NET SDK Help

   Remarks

The IWorkspaceFactoryStatus::WorkspaceStatus property returns an enumeration of IWorkspaceStatus objects for all currently open workspaces.

0 Kudos