locked
The question about vectors/arrays in C++CX

    General discussion

  • Hello. Im begin study C++/CX and have first question. In this documentation http://msdn.microsoft.com/en-us/library/windows/apps/hh700131.aspx (in the end of article) I explore that array pointer handle returning in property, is not recommended because of returning a whole array(resource consuming). Im creating test blank app and add some code for test.

     

    using namespace Platform;
    using namespace Platform::Collections;
    using namespace Windows::Foundation::Collections;

    public enum class ColorEnum { Red,Green,Blue }; public ref class TestClass sealed { private: Vector<ColorEnum>^ colorArr; public: property IVector<ColorEnum>^ ColorArray { IVector<ColorEnum>^ get(){return colorArr;} void set(IVector<ColorEnum>^value) { int len = static_cast<int>(value->Size); for(unsigned int i = 0 ; i < len; i++) { colorArr->SetAt(i,value->GetAt(i)); } } } };

    //When im call this code after InitializeComponent
    
    tClass = ref new TestClass();	
    tClass->ColorArray = safe_cast<IVector<ColorEnum>^>(ref new Vector<ColorEnum>(20));


    im getting runtime error. Whats wrong, and how write good code for situation like this? - its a question for professionals.

    Ahhh.. i was stupid.

    TestClass()
    		{
    			colorArr = ref new Vector<ColorEnum>();
    		}
    
    		 property IVector<ColorEnum>^ ColorArray
    		 {  
    			 IVector<ColorEnum>^ get(){return colorArr;}
    			 void set(IVector<ColorEnum>^value)
    			 { 
    				 unsigned int len = static_cast<unsigned int>(value->Size);
    				 colorArr->Clear();
    				 for(unsigned int i = 0 ; i < len; i++)
    				 {
    					 colorArr->Append(value->GetAt(i));
    				 }
    			 }
    		 } 
     This seems correct now..

    But if somebody have advise - I accept it.






    • Edited by ChaosDev Sunday, October 14, 2012 6:36 AM
    • Changed type Jesse Jiang Friday, October 19, 2012 6:38 AM
    Sunday, October 14, 2012 6:14 AM

All replies

  • I think the solution is good.
    Do you have any question about this?

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 15, 2012 6:25 AM