locked
foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator RRS feed

  • Question

  • when i look at the amibroker documentation
    AmiBroker Object Model

    Stocks

    Methods:

    • Function Add (ByVal Ticker As String) As Object
    • Function GetTickerList (ByVal nType As Long) As String
    • Function Remove (ByVal Item As Variant) As Boolean

    Properties:

    • Item (ByVal Item As Variant) As Object [r/o] [default]
    • Count As Long

    Description:

    Stocks is a collection of Stock objects. It is available as a property of Application object.

    so i am trying the following code.
                      Type brokerType;
                        object brokerObject;
                        brokerType = Type.GetTypeFromProgID("Broker.Application");
                        brokerObject = Activator.CreateInstance(brokerType);
                        object activedoc = brokerType.InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, brokerObject, null);
                          label1.Text = (string)brokerType.InvokeMember("Name", BindingFlags.GetProperty, null, activedoc, null);
                          label2.Text = (string)brokerType.InvokeMember("DatabasePath", BindingFlags.GetProperty, null, brokerObject, null);
                          object stks = brokerType.InvokeMember("Stocks", BindingFlags.GetProperty, null, brokerObject, null);
                          foreach (object stk in stks)
                          {
                              object quots = brokerType.InvokeMember("Quotations", BindingFlags.GetProperty, null, stk, null);
                          }
    i get an error around the foreach stating "foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator"

    because i am binding using reflection, how do i tell c# that stks is a collection.

    Sunday, May 17, 2009 3:29 PM

Answers

  • Alternately you can just use the full type name of IEnumerable:

    System.Collections.IEnumerable stksEnum = stks as System.Collections.IEnumerable;
    Sunday, May 17, 2009 3:47 PM
  • The reason for the error message is that you already had the alias:
    using System.Collections.Generic;

    So when the compiler saw IEnumerable, it assumed you meant System.Collections.Generic.IEnumerable<T>. We don't need the generic here, we just want the non-generic version of IEnumerable which is System.Collections.IEnumerable.
    Sunday, May 17, 2009 3:49 PM

All replies

  • IEnumerable stksEnum = stks as IEnumerable;
    if (stksEnum != null)
        foreach (object stk in stksEnum)
        {
        }
    Sunday, May 17, 2009 3:41 PM

  • i get the following error.
      Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires '1' type arguments  
    what type should i use here.
    Sunday, May 17, 2009 3:45 PM
  • using System.Collections.Generic;
    Sunday, May 17, 2009 3:45 PM
  • oops, that's:
    using System.Collections;
    Sunday, May 17, 2009 3:46 PM
  • Alternately you can just use the full type name of IEnumerable:

    System.Collections.IEnumerable stksEnum = stks as System.Collections.IEnumerable;
    Sunday, May 17, 2009 3:47 PM
  • The reason for the error message is that you already had the alias:
    using System.Collections.Generic;

    So when the compiler saw IEnumerable, it assumed you meant System.Collections.Generic.IEnumerable<T>. We don't need the generic here, we just want the non-generic version of IEnumerable which is System.Collections.IEnumerable.
    Sunday, May 17, 2009 3:49 PM
  • thanks a lot. u have been a great help
    Sunday, May 17, 2009 3:50 PM
  • I would mark my first reply as the answer (only that one), and if you like, mark the other two as helpful. That way any future visitors get the correct answer first.
    Sunday, May 17, 2009 3:54 PM