Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4001074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:51:37+00:00 2026-05-20T07:51:37+00:00

Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator

  • 0

Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator in C++/CLI? Alternatively, does someone know how to fix the following code from MS Connect which does not compile in Visual Studio 2005?

http://connect.microsoft.com/VisualStudio/feedback/details/101089/how-to-implement-ienumerable-t-and-ienumerable-c-cli

using namespace System;
using namespace System::Collections::Generic;

generic <class T>
public ref struct MyArray : public IEnumerable<T>
{    

    MyArray( array<T>^ d )
    {
        data = d;
    }
    ref struct enumerator : IEnumerator<T>
    {
        enumerator( MyArray^ myArr )
        {
            colInst = myArr;
            currentIndex = -1;
        }

        bool MoveNext()
        {
            if( currentIndex < colInst->data->Length - 1 )
            {
                currentIndex++;
                return true;
            }
            return false;
        }

        property T Current
        {
            T get()
            {
                return colInst->data[currentIndex];
            }
        };
        // This is required as IEnumerator<T> also implements IEnumerator
        property Object^ Current2
        {
            virtual Object^ get() new sealed = System::Collections::IEnumerator::Current::get
            {
                return colInst->data[currentIndex];
            }
        };

        void Reset() {}
        ~enumerator() {}

        MyArray^ colInst;
        int currentIndex;
    };

    array<T>^ data;

    IEnumerator<T>^ GetEnumerator()
    {
        return gcnew enumerator(this);
    }

    virtual System::Collections::IEnumerator^ GetEnumerator2() new sealed = System::Collections::IEnumerable::GetEnumerator
    {
        return gcnew enumerator(this);
    }
};

int main()
{
    int retval = 0;

    MyArray<int>^ col = gcnew MyArray<int>( gcnew array<int>{10, 20, 30 } );

    for each( Object^ c in col )
    {
        retval += (int)c;
    }
    retval -= 10 + 20 + 30;

    Console::WriteLine("Return Code: {0}", retval );
    return retval;
}

The compiler is unable to find the enumerator method implementations:

error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'bool System::Collections::IEnumerator::MoveNext(void)'   c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'void System::Collections::IEnumerator::Reset(void)'  c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'T System::Collections::Generic::IEnumerator<T>::Current::get(void)'  c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>' must provide an implementation for the interface method 'System::Collections::Generic::IEnumerator<T> ^System::Collections::Generic::IEnumerable<T>::GetEnumerator(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  68  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:51:38+00:00Added an answer on May 20, 2026 at 7:51 am

    This compiles for me without a single warning (on VS2010):

    using namespace System;
    using namespace System::Collections::Generic;
    
    generic <class T>
    public ref struct MyArray : public IEnumerable<T>
    {    
    
        MyArray( array<T>^ d )
        {
            data = d;
        }
        ref struct enumerator : IEnumerator<T>
        {
            enumerator( MyArray^ myArr )
            {
                colInst = myArr;
                currentIndex = -1;
            }
    
            virtual bool MoveNext() = IEnumerator<T>::MoveNext
            {
                if( currentIndex < colInst->data->Length - 1 )
                {
                    currentIndex++;
                    return true;
                }
                return false;
            }
    
            property T Current
            {
                virtual T get() = IEnumerator<T>::Current::get
                {
                    return colInst->data[currentIndex];
                }
            };
            // This is required as IEnumerator<T> also implements IEnumerator
            property Object^ Current2
            {
                virtual Object^ get() = System::Collections::IEnumerator::Current::get
                {
                    return colInst->data[currentIndex];
                }
            };
    
            virtual void Reset() = IEnumerator<T>::Reset {}
            ~enumerator() {}
    
            MyArray^ colInst;
            int currentIndex;
        };
    
        array<T>^ data;
    
        virtual IEnumerator<T>^ GetEnumerator()
        {
            return gcnew enumerator(this);
        }
    
        virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
        {
            return gcnew enumerator(this);
        }
    };
    
    int main()
    {
        int retval = 0;
    
        MyArray<int>^ col = gcnew MyArray<int>( gcnew array<int>{10, 20, 30 } );
    
        for each( Object^ c in col )
        {
            retval += (int)c;
        }
        retval -= 10 + 20 + 30;
    
        Console::WriteLine("Return Code: {0}", retval );
        return retval;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a working fix for the ie z-index bug? I have
Does anyone have any experience with doing this? I'm working on a Java decompiler
Does anyone have a working setup for hunspell and Emacs ? Simply setting ispell-program-name
Does anyone have experience working with language workbench tools such as Xtext, Spoofax, and
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone have a definitive answer to whether Sql Server Management Objects is compatible
Does anyone have the secret formula to resizing transparent images (mainly GIFs) without ANY
Does anyone have a technique for generating SQL table create (and data insert) commands
Does anyone have a good way to build MSI (vdproj) projects using MsBuild or
Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.