Error instancing a FileGDBWorkkspaceFactotyClass

3921
4
10-07-2010 07:32 AM
PedroAbreu
New Contributor II
Hi,

With ArcGIS Desktop 10, Sometimes this line of code:
IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass();


throws this exception:
Unable to cast object of type System.__ComObject to type ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass


I'm puzzled... Anybody has experienced this yet?
0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
If you're running this code in a standalone application then you need to check out a license prior to running any ArcObjects code.  You also shouldn't be using New to create an instance of the workspace factory.  A workspace factory is a singleton object and should be instantiated using the Activator class.

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Interacting_with_single...
Vidar
by
Occasional Contributor II

Thank you Neil - you've helped me on and off for 20 years now!!

0 Kudos
NattawutWiginiyatanee1
New Contributor II

Yep! Thank you. this helped. But I still wonder most of my projects. this code won't break.

IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass();  

But for my current project it breaks. And Neil Clemmons answer solves the problem.

0 Kudos
PedroAbreu
New Contributor II
Neil,

I've been coding ArcObjects for so many years, and never had a problem with instancing Workspaces Factories with the "new" keyword.
But there's always a first for everything, and this was my first.

Thank you for your help 😉
0 Kudos