locked
Creating a managed array of managed structs containing managed elements RRS feed

  • Question

  • OK, I am trying to create an array of structs.

     

     

    namespace AB
    {
    
    public ref class ABClass : public System::Windows::Forms::Form
    {
    // Class is here
    }
    
    public value struct StrStruct
    {
    	String^ Str1;
    	String^ Str2;
    };
    
    }
    
    
    

     


    Later, within a member function of the ABClass, I try to allocate an array of these structs:

    void AB::ABClass::HandleStrings ()
    {
     int arrCount = 0;
    
    // code here that increments arrCount
    
    // Create array of AB::StrStruct here
    Array ^SSS = Array::CreateInstance (StrStruct, arrCount); // DOES NOT WORK
    }
    


    Gives error error C2275: 'AB::StrStruct' : illegal use of this type as an expression

    I think I'm tackling this the wrong way.  I'm open to other suggestions.  I do need an array of structures that can be sized at runtime, and the structures need to have managed strings (and other managed elements).

    Any pointers?

     

    NJ

     


    Sunday, July 31, 2011 6:37 AM

Answers

  • What you are lookingf for is probably cli::array:

     

    cli::array<StrStruct> ^SSS = gcnew cli::array<StrStruct>(arrCount);
    
    

     


    • Edited by Mike Danes Sunday, July 31, 2011 8:26 AM
    • Marked as answer by NetJohnP Sunday, July 31, 2011 10:10 PM
    Sunday, July 31, 2011 7:59 AM

All replies

  • What you are lookingf for is probably cli::array:

     

    cli::array<StrStruct> ^SSS = gcnew cli::array<StrStruct>(arrCount);
    
    

     


    • Edited by Mike Danes Sunday, July 31, 2011 8:26 AM
    • Marked as answer by NetJohnP Sunday, July 31, 2011 10:10 PM
    Sunday, July 31, 2011 7:59 AM
  • What you are lookingf for is probably cli::array:

     

    cli::array<StrStruct> ^SSS = cli::array<StrStruct>(arrCount);
    
    

     

    That gave me a "error C2440: '<function-style-cast>' : cannot convert from 'int' to 'cli::array<Type>'"

    Even tried the AB::StrStruct just in case it was a namespace issue.

     

    NJ

     

    Sunday, July 31, 2011 8:18 AM
  • Oops, I omitted a gcnew. Plese read the updated example.
    • Marked as answer by NetJohnP Sunday, July 31, 2011 10:10 PM
    • Unmarked as answer by NetJohnP Sunday, July 31, 2011 10:10 PM
    Sunday, July 31, 2011 8:27 AM