I have a function which parses one string into two strings. In C# I would declare it like this:
我有一个函数将一个字符串解析为两个字符串。在c#中,我会这样声明:
void ParseQuery(string toParse, out string search, out string sort)
{
...
}
and I'd call it like this:
我这样称呼它:
string searchOutput, sortOutput;
ParseQuery(userInput, out searchOutput, out sortOutput);
The current project has to be done in C++/CLI. I've tried
当前项目必须在c++ /CLI中完成。我试过了
using System::Runtime::InteropServices;
...
void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort)
{
...
}
but if I call it like this:
但如果我这样称呼它:
String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, [Out] searchOutput, [Out] sortOutput);
I get a compiler error, and if I call it like this:
我得到一个编译错误,如果我这样称呼它:
String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, searchOutput, sortOutput);
then I get an error at runtime. How should I declare and call my function?
然后在运行时出现错误。如何声明和调用函数?
3 个解决方案
#1
70
C++/CLI itself doesn't support a real 'out' argument, but you can mark a reference as an out argument to make other languages see it as a real out argument.
c++ /CLI本身并不支持真正的“out”参数,但您可以将引用标记为out参数,以使其他语言将其视为真正的out参数。
You can do this for reference types as:
对于引用类型,您可以这样做:
void ReturnString([Out] String^% value)
{
value = "Returned via out parameter";
}
// Called as
String^ result;
ReturnString(result);
And for value types as:
值类型为:
void ReturnInt([Out] int% value)
{
value = 32;
}
// Called as
int result;
ReturnInt(result);
The % makes it a 'ref' parameter and the OutAttribute marks that it is only used for output values.
%使它成为一个'ref'参数,而OutAttribute标记它只用于输出值。
#2
8
Using Visual Studio 2008, this works and solved a major problem at my job. Thanks!
使用Visual Studio 2008,它可以解决我工作中的一个主要问题。谢谢!
// 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);
}
}
}
#3
0
It's not supported. The closest you can get is ref
它不支持。你能得到的最接近的是ref
Granted you can fake it, but you lose a compile time check.
假设您可以伪造它,但是您丢失了编译时间检查。
#1
70
C++/CLI itself doesn't support a real 'out' argument, but you can mark a reference as an out argument to make other languages see it as a real out argument.
c++ /CLI本身并不支持真正的“out”参数,但您可以将引用标记为out参数,以使其他语言将其视为真正的out参数。
You can do this for reference types as:
对于引用类型,您可以这样做:
void ReturnString([Out] String^% value)
{
value = "Returned via out parameter";
}
// Called as
String^ result;
ReturnString(result);
And for value types as:
值类型为:
void ReturnInt([Out] int% value)
{
value = 32;
}
// Called as
int result;
ReturnInt(result);
The % makes it a 'ref' parameter and the OutAttribute marks that it is only used for output values.
%使它成为一个'ref'参数,而OutAttribute标记它只用于输出值。
#2
8
Using Visual Studio 2008, this works and solved a major problem at my job. Thanks!
使用Visual Studio 2008,它可以解决我工作中的一个主要问题。谢谢!
// 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);
}
}
}
#3
0
It's not supported. The closest you can get is ref
它不支持。你能得到的最接近的是ref
Granted you can fake it, but you lose a compile time check.
假设您可以伪造它,但是您丢失了编译时间检查。