locked
How can i write reference parameter in WinRT?

    Question


  • void test(int& id) {

    id =5; }

    How can i  write reference parameter in WinRT? 

    Thursday, November 8, 2012 4:49 AM

Answers

  • You can not have reference parameters (which could be in/out) on the public surface of a C++/CX component but you can have out parameters in WinRT. The following methods can be public methods on a public ref class:

    void func(int* id)
    {
        if (id != nullptr) *id = 5;
    }
    
    void func(IVector<int>^* ids)
    {
        if (ids != nullptr) 
        {
             *ids = ref new Platform::Vector<int>();
             (*ids)->Append(10);
             (*ids)->Append(20);
        }
    }
    
    Hope this helps,
    Marian Luparu
    Visual C++
    Thursday, November 8, 2012 7:00 PM

All replies

  • Hi,

    you can not pass the reference between Windows Runtime components.
    The public type in components should be Windows Runtime types. We cannot pass a native type in components.

    Please check this document
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh441569.aspx

    Go through this thread also - http://social.msdn.microsoft.com/Forums/en/winappswithnativecode/thread/5bc607c4-1a5e-4051-abba-b52691938a57

    thanks,
    Bhash

    Thursday, November 8, 2012 4:48 PM
  • You can not have reference parameters (which could be in/out) on the public surface of a C++/CX component but you can have out parameters in WinRT. The following methods can be public methods on a public ref class:

    void func(int* id)
    {
        if (id != nullptr) *id = 5;
    }
    
    void func(IVector<int>^* ids)
    {
        if (ids != nullptr) 
        {
             *ids = ref new Platform::Vector<int>();
             (*ids)->Append(10);
             (*ids)->Append(20);
        }
    }
    
    Hope this helps,
    Marian Luparu
    Visual C++
    Thursday, November 8, 2012 7:00 PM
  • Hi .DotF:

    you can not pass the reference between Windows Runtime components.

    But if you want to pass the reference in the same component.

    You can do like this

    public ref class class1

    {

    internal:
        void fun1(int &b);

    };

    • Proposed as answer by Jumping fish Saturday, November 10, 2012 12:48 AM
    Friday, November 9, 2012 1:08 AM