Pregunta

Tengo una función que analiza una cadena en dos cadenas. En C # lo declararía así:

void ParseQuery(string toParse, out string search, out string sort)
{
    ...
}

y lo llamaría así:

string searchOutput, sortOutput;
ParseQuery(userInput, out searchOutput, out sortOutput);

El proyecto actual tiene que hacerse en C ++ / CLI. Lo he intentado

using System::Runtime::InteropServices;

...

void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort)
{
    ...
}

pero si lo llamo así:

String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, [Out] searchOutput, [Out] sortOutput);

Me sale un error del compilador, y si lo llamo así:

String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, searchOutput, sortOutput);

entonces recibo un error en tiempo de ejecución. ¿Cómo debo declarar y llamar a mi función?

¿Fue útil?

Solución

C ++ / CLI en sí mismo no admite un argumento de "salida" real, pero puede marcar una referencia como un argumento de salida para que otros lenguajes lo vean como un argumento de salida real.

Puede hacer esto para tipos de referencia como:

void ReturnString([Out] String^% value)
{
   value = "Returned via out parameter";
}

// Called as
String^ result;
ReturnString(result);

Y para tipos de valor como:

void ReturnInt([Out] int% value)
{
   value = 32;
}

// Called as
int result;
ReturnInt(result);

El% lo convierte en un parámetro 'ref' y OutAttribute marca que solo se usa para valores de salida.

Otros consejos

Con Visual Studio 2008, esto funciona y resuelve un problema importante en mi trabajo. Gracias!

// header
// Use namespace for Out-attribute.
using namespace System::Runtime::InteropServices; 
namespace VHT_QMCLInterface {
   public ref class Client
   {
    public:
        Client();
        void ReturnInteger( int a, int b, [Out]int %c);
        void ReturnString( int a, int b, [Out]String^ %c);
   }
}

// cpp
namespace VHT_QMCLInterface {

    Client::Client()
    {

    }

    void Client::ReturnInteger( int a, int b, [Out]int %c)
    {
        c = a + b;
    }
    void Client::ReturnString( int a, int b, [Out]String^ %c)
    {
        c = String::Format( "{0}", a + b);
    }
}

// cs
namespace TestQMCLInterface
{
    class Program
    {
        VHT_QMCLInterface.Client m_Client = new VHT_QMCLInterface.Client();
        static void Main(string[] args)
        {
            Program l_Program = new Program();
            l_Program.DoReturnInt();
            l_Program.DoReturnString();
            Console.ReadKey();
        }

        void DoReturnInt()
        {
            int x = 10;
            int y = 20;
            int z = 0;
            m_Client.ReturnInteger( x, y, out z);
            Console.WriteLine("\nReturnInteger: {0} + {1} = {2}", x, y, z);
        }

        void DoReturnString()
        {
            int x = 10;
            int y = 20;
            String z = "xxxx";
            m_Client.ReturnString(x, y, out z);
            Console.WriteLine("\nReturnString: {0} + {1} = '{2}'", x, y, z);
        }
     }
}

No es compatible. Lo más cercano que puede obtener es ref

De acuerdo, puedes fingirlo, pero pierdes una verificación de tiempo de compilación.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top