Why is casting Object^ to Box<Rect>^ not allowed? RRS feed

  • Question

  • There is a little difference between the following two code segments ,but the first is correct and the second is failed.


    using namespace Platform;

    using namespace Windows::Foundation;

    ref class MyData { public: MyData(Object^ d) :m_Data(d) { } property Object^ data { Object^ get() { return m_Data; } } private: Object^ m_Data; };

    //segment 1

    int n=100;

    Box<int>^ bn=n;//type is int
    MyData^ d=ref new MyData(bn);
    Box<int>^ out=dynamic_cast<Box<int>^>(d->data);
    if( out )
      //It is here



    //segment 2

    Rect rc=RectHelper::FromCoordinatesAndDimensions(0,0,100,100) Box<Rect>^ bn=rc;//type is rect MyData^ d=ref new MyData(bn); Box<Rect>^ out=dynamic_cast<Box<Rect>^>(d->data); if( out ) { } else {

    //It is here


    Wednesday, April 18, 2012 12:55 AM


All replies

  • Hello,

    Thanks for your feedback. I find this is rect's problem, If I change it as Point and it works fine.

    	Rect rc=Rect(0,0,100,100);
    	Point pt=Point(1.0,2.0);
    	Box<Point>^ bp=pt;
    	Box<Rect>^ bn=rc;
    		Rect rc1=bn->Value;  // Throw "No such interface supported\r\n"
    		Point pt2=bp->Value;  // No error throw
    	}catch(Exception^ ex)
    		String^ res=ex->Message;

    I will involve more experts to investigate it.

    Best regards,

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

    Thursday, April 19, 2012 9:35 AM
  • There might be more problem with rect:

    if i pass the return value of function RectHelper::FromCoordinatesAndDimensions to function as a parameter,it seems not to work as expected.

    However,it is correct if i save the return value of function RectHelper::FromCoordinatesAndDimensions to a temporary variable value and pass it to other function .

    Tuesday, April 24, 2012 5:58 AM
  • Thanks for the feedback. I've reproduced these issues in Consumer Preview with the Dev11 beta and also confirmed that they do not reproduce in current builds. Do you have workarounds for the boxing issue?


    <edit: Verified this doesn't repro in RC>
    Wednesday, April 25, 2012 1:34 AM
  • Maybe I might create my rect struct instead,but i have not attempt.
    Saturday, April 28, 2012 12:16 AM