locked
How to pass Generic::List^ to function in CLI/C++ ? RRS feed

  • Question

  • Hello,

    how can I pass generic list to a function? Example below does NOT work properly.

     

       15 public ref class A

       16 {

       17 public:

       19 

       20   A{}

       21   {

       22   }

       23   ~A{}

       24   {

       25   }

       26 

       27 private: System::Void use( System::Collections::Generic::List^ list ){

       28 

       29            // working with generic list "genericList" from class B

       30 

       31          }//

       32 

       33 }; // public ref class A

       34 

       35 public ref class B

       36 {

       37 public:

       38   List<Object^>^ genericList;

       39 

       40   B{}

       41   {

       42    genericList = gcnew List<Object^>();

       43   }

       44   ~B{}

       45   {

       46    delete( genericList );

       47   }

       48 

       49 private: System::Void button1_Click(){

       50 

       51          genericList->Add( ... );

       52 

       53          A a = gcnew A();

       54          A->use( genericList ); // ERROR C2664

       55 

       56          delete( a )

       57 

       58          }//

       59 

       60 }; // public ref class B

     

    many thanks.

     



    • Edited by Jerry.Mouse Thursday, June 9, 2011 7:26 PM code repair
    Thursday, June 9, 2011 7:08 PM

Answers

  • You need to specify the generic type for the method, ie: List<Object^>^

     

    private: System::Void use( System::Collections::Generic::List<Object^>^ list )
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Jerry.Mouse Thursday, June 9, 2011 7:34 PM
    Thursday, June 9, 2011 7:15 PM

All replies

  • You need to specify the generic type for the method, ie: List<Object^>^

     

    private: System::Void use( System::Collections::Generic::List<Object^>^ list )
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Jerry.Mouse Thursday, June 9, 2011 7:34 PM
    Thursday, June 9, 2011 7:15 PM
  • finally, its working. :-)

    • Edited by Jerry.Mouse Thursday, June 9, 2011 7:33 PM update
    Thursday, June 9, 2011 7:28 PM