locked
How do you pass a struct by reference to a WinRT compononet?

    Question

  • How can I pass a struct by reference to a method in a WinRT Component?

    For example, something like this:

    public value struct MyStruct
    {
       int x;
       int y;
    }
    
    public ref class MyWinRTClass sealed
    {
       public MyWinRTClass(){};
       public void UpdateMyStruct(MyStruct * struct)
       {
          struct->x = 100;
          struct->y = 200;
       } 
    }

    I've tried the above, but when I try to consume that component as show below, the values of the struct inside the method are always 0:

    MyWinRTClass comp = new MyWinRTClass();
    MyStruct myStruct;
    myStruct.x = 50;
    myStruct.y = 100;
    comp.UpdateMyStruct(out myStruct);
    _textBlock.Text = myStruct.x.ToString();

    Saturday, May 12, 2012 4:02 AM

All replies

  • Hi,

    I created a C++ WinRT component and cannot reproduce this issue in a C# Metro app. 'mystruct' variable changed correctly by UpdateMyStruct method.

    Since 'struct' is keyword, please change the signature of UpdateMyStruct method as the following sample code.

    Here is sample code for verification.

    public ref class MyWinRTClass sealed
    {
    public:
    	MyWinRTClass(void){};
    	~MyWinRTClass(void){};
    
    	void UpdateMyStruct(MyStruct * mystruct)
       {
    	  mystruct->x = 100;
    	  mystruct->y = 200;
       } 
    };


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 14, 2012 6:48 AM
    Moderator
  • Perhaps I wasn't clear in my first post.  Let me try again.

    The values in the struct that are passed into the method are always 0.

    Please try the following:

    public ref class MyWinRTClass sealed
    {
    public:
    MyWinRTClass(void){};
    ~MyWinRTClass(void){};

    void UpdateMyStruct(MyStruct * mystruct)
      
    {
      mystruct
    ->x = mystruct->x +100;
      mystruct
    ->y = mystruct->y + 200;
      
    }
    };

    Monday, May 14, 2012 1:08 PM
  • I think I've found a solution.
    I believe we have to use public ref struct instead.

    public ref struct MyStruct
    {
       private: int _x;
       public: property int X
       {
          int get() { return _x; }
       }
    }
    

    • Marked as answer by BullyOwner Monday, May 14, 2012 4:07 PM
    • Unmarked as answer by BullyOwner Monday, May 14, 2012 6:16 PM
    Monday, May 14, 2012 4:06 PM
  • "A reference struct (ref struct) is the same as a ref class, except that a ref struct declares a runtime class that by default has public accessibility."
    Monday, May 14, 2012 4:54 PM
  • Well, this has created another problem.

    Now, when I try to add a method like the following to the public ref struct, the compiler gives an "INTERNAL COMPILER ERROR":

       public:
          WinRTRefStruct FromXY(int x, int y)
          {
             WinRTRefStruct s;
             s._x = x;
             s._y = y;
             return s;
          }
    

     
    Monday, May 14, 2012 5:05 PM