1

I have been working in the next code to get all Subtypes from a standalone table in a single string that concatenates it. I work with ArcMap and arcobjects library in C# (sharp) lenguage.

But I can not put it to works it correctly.

My actual code:

IDocument docRDC = m_application.Document;
IMxDocument mxdocRDC = docRDC as IMxDocument;

string esFeature = "";
string esTable = "";

if (mxdocRDC.SelectedItem is IFeatureLayer2)
    {
    esFeature = "Si";
    MessageBox.Show("Es IFeaturelayer2");
    }

if (mxdocRDC.SelectedItem is ITable)
    {
    esTable = "Si";
    MessageBox.Show("Es Itable");
    }

ITable table = mxdocRDC.SelectedItem as ITable;

System.Text.StringBuilder listadosubtipos = new System.Text.StringBuilder();

if (esFeature != "Si" && esTable=="Si")
            {
                IObjectClass MiObjeClassTabla = table as IObjectClass;
                ISubtypes MisSubtypesTabla = (ISubtypes)MiObjeClassTabla;
                IEnumSubtype enumSubtypeTabla;
                int subtypeCodeTabla;
                string subtypeNameTabla;


            if (MisSubtypesTabla.HasSubtype)
                {
                enumSubtypeTabla = MisSubtypesTabla.Subtypes;
                subtypeNameTabla = enumSubtypeTabla.Next(out subtypeCodeTabla);

                while (subtypeNameTabla != null)
                    {
                    listadosubtipos.Append(subtypeNameTabla);
                    listadosubtipos.Append(", ");
                    subtypeNameTabla = enumSubtypeTabla.Next(out subtypeCodeTabla);
                    }
                }

                textBox3.Text = Convert.ToString(listadosubtipos);
            }
  • 1
    Are you getting an error? Are the results not what you expect? What is the problem? – Rich Wawrzonek Oct 24 at 0:36
  • when I run the application; it just executes until they reach the row 25 (IObjectClass MiObjeClassTabla = table as IObjectClass;) and then nothing happens. – Rdcalero Oct 25 at 20:28
0

IFeatureLayer does not implement IObjectClass but IFeatureLayer.FeatureClass does, which is why your cast to IObjectClass MiObjeClassTabla is Null. Likewise if your mxdocRDC.SelectedItem is a standalone table is likely to be of type ITableView, from which you can access the underlying table using (mxdocRDC.SelectedItem as ITableView).Table.

I would suggest starting the code like this:

ITable table=null; // initialize the table to nothing 

if (mxdocRDC.SelectedItem is IFeatureLayer)
{
    esFeature = "Si";
    MessageBox.Show("Es IFeaturelayer2");
    // IFeatureClass implements ITable so a direct cast should be no problem
    table = (ITable) (mxdocRDC.SelectedItem as IFeatureLayer).FeatureClass; 
}

if (mxdocRDC.SelectedItem is ITableView)
{
    esTable = "Si";
    MessageBox.Show("Es Itable");
    table = (mxdocRDC.SelectedItem as ITableView).Table;
}

if (null != table)
{
    // do the rest of your code here because a valid datasource found
}

But beware, a layer or table view can be invalid, indicated by a red exclamation mark. For interfaces that implement ILayer you can test that the layer.Valid before continuing, giving you a chance to exit gracefully before the program proceeds to crash and burn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.