Microsoft KB Archive/823936

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

Article ID: 823936

Article Last Modified on 1/4/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



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&quot;One&quot;,S&quot;Two&quot;,S&quot;Three&quot;}; IEnumerable* myEnum = dynamic_cast (myStringArray);



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



Steps to Reproduce the Behavior
 Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. 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.  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;   Modify the main function as follows: int _tmain {   String* myStringArray[]= {S&quot;One&quot;,S&quot;Two&quot;,S&quot;Three&quot;} ; 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:  Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li> On the Build menu, click Build Solution. You may receive the error message that the &quot;Symptoms&quot; section describes.</li></ol>

Keywords: kberrmsg kblangcpp kbprb kbcollections kbmanaged KB823936

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.