Object type problem in VS

Hi,

i dont understand the autocomplete in visual studio.

foreach (GH_Cluster cli in adoc.Objects.OfType<GH_Cluster>())
                {
                    cli.CollectData();                   
                }

VS recognize that cli is a object of type GH_Cluster and all methodes for this type are available.
But something like the following dont work for the autocomplete options.It is still a IGH_DocumentObject in the if statment but it works if i compile it.

foreach (var cli in adoc.Objects)
{
    if (cli.GetType() is GH_Cluster)
    {
        cli.CollectData();
    }
}

|Error|CS1061|‘IGH_DocumentObject’ does not contain a definition for ‘CollectData’ and no accessible extension method ‘CollectData’ accepting a first argument of type ‘IGH_DocumentObject’ could be found (are you missing a using directive or an assembly reference?)

I am confused maybe someone can shine light in my unknown darkness.

foreach (var obj in adoc.Objects)
{
    if (obj is GH_Cluster cli)
    {
        cli.CollectData();
    }
}

C# is strongly-typed. A inferrable variable doesn’t mean methods from the inferred type can be directly used.

1 Like