Error C2440 occurs when you convert a string array to the IEnumerable* interface implicitly in Visual C++ .NET or in Visual C++ 2005 (823936)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

SYMPTOMS

The IEnumerable interface is used to expose the enumerator of a collection class. The enumerator supports a simple iteration over a collection. When you declare a string array, and you want to enumerate its items, you can use the IEnumerable interface for the iteration. However, when you convert a string array to the IEnumerable* interface implicitly, you may receive the following error message:
error C2440: 'initializing' : cannot convert from 'System::String __gc * __gc[]' to 'System::Collections::IEnumerable __gc *'

RESOLUTION

To resolve this problem, use the dynamic_cast <> operator to perform type casting. The modified code is as follows:
String* myStringArray[]= {S"One",S"Two",S"Three"};
IEnumerable* myEnum = dynamic_cast<IEnumerable*> (myStringArray);

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Expand Visual C++ Projects under Project Types, click (.NET), and then click Console Application (.NET) under Templates.

    Note In Visual Studio 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates.
  4. Use the using directive on the System::Collections namespace so that you do not have to qualify declarations from these namespaces later in your code. You must use the following statements before any other declarations:
    using namespace System::Collections;
    
  5. Modify the main() function as follows:
    int _tmain()
    {
        String* myStringArray[]= {S"One",S"Two",S"Three"} ;
        IEnumerable* myEnum = myStringArray;  
    
        return 0;
    }
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  6. On the Build menu, click Build Solution. You may receive the error message that the "Symptoms" section describes.

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kberrmsg kbManaged kbLangCPP kbCollections kbprb KB823936 kbAudDeveloper