Raster Catalog creation within a GDB

09-06-2011 08:37 PM
New Contributor
Hey all:  I have been away from writing code for a little while.  Would someone want to tell me why they think the following code would be breaking?  Two things to note:  One is that the IFieldsEdit does not give me a method AddField(IField) that I would suspect but rather a SetField(int, IField) method.  Second, the call to create the raster catalog is failing with an error about trying to write to protected memory.  I have seen the latter error before, but I don't remember how to fix it.  Any guidance?  Thanks in advance!

// Open a workspace factory and create a new file GDB.
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory fileGdbWSF = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
            IWorkspaceName fileGdbWSName = fileGdbWSF.Create("C:\\Data", "Testing5.gdb", null, 0);

            IName wsName = (IName)fileGdbWSName;
            IWorkspace fileGdbWS = (IWorkspace)wsName.Open();

            IRasterWorkspaceEx rasWS = (IRasterWorkspaceEx)fileGdbWS;

            IFields fields = new Fields();
            IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
            fieldsEdit.FieldCount_2 = 2;

            IField field1 = new Field();
            IFieldEdit fieldEdit1 = (IFieldEdit)field1;

            IField field2 = new Field();
            IFieldEdit fieldEdit2 = (IFieldEdit)field2;

            fieldEdit1.Name_2 = "FieldOne";
            fieldEdit1.AliasName_2 = "Field One";
            fieldEdit1.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit1.Editable_2 = true;
            fieldEdit1.IsNullable_2 = true;
            fieldEdit1.Length_2 = 20;
            fieldEdit2.Name_2 = "FieldTwo";
            fieldEdit2.AliasName_2 = "Field One";
            fieldEdit2.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit2.Editable_2 = true;
            fieldEdit2.IsNullable_2 = true;
            fieldEdit2.Length_2 = 20;
            // Why are these methods here instead of AddField(IField)?
            fieldsEdit.set_Field(0, field1);
            fieldsEdit.set_Field(1, field2);

            rasWS.CreateRasterCatalog("MyCatalog", fields, "Shape", "Raster", null);
0 Kudos
0 Replies