Click to See Complete Forum and Search --> : Does the method parameter keyword: ref, out, params be used in c++/cli?


POla
March 13th, 2008, 12:02 AM
Since C# is similar to C so I alway make it as reference. Whenever I borrow codes from C# to C++/CLI and see the parameter keyword: ref, out, params, I will delete it, so that no error occur. By doing this, is there any problem? If yes, what should i use as the same meaning to these 3 keywords?

David Anton
March 13th, 2008, 07:13 PM
I'd be surprised if you didn't run into problems by just deleting them - they should be converted to the CLI equivalents.

For the following C# example:
public void test(ref foo param1, out foo param2, params object[] param3)
{
}

The C++/CLI equivalent is generated from Instant C++ as:
public:
void test(foo ^%param1, [System::Runtime::InteropServices::Out] foo ^%param2, ... array<System::Object^> ^param3)
{
}

This is assuming that 'foo' is a reference type (i.e., ref class). If not, then just remove the 'hats' for the first two parameters.

POla
March 17th, 2008, 03:27 AM
I'd be surprised if you didn't run into problems by just deleting them - they should be converted to the CLI equivalents.

For the following C# example:
public void test(ref foo param1, out foo param2, params object[] param3)
{
}

The C++/CLI equivalent is generated from Instant C++ as:
public:
void test(foo ^%param1, [System::Runtime::InteropServices::Out] foo ^%param2, ... array<System::Object^> ^param3)
{
}

This is assuming that 'foo' is a reference type (i.e., ref class). If not, then just remove the 'hats' for the first two parameters.

For c# code :
class Packet{};

class Form1{
EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
public void test(ref remoteEP, out array[byte] bydata){}
}

How should i convert? Just concern the ref and out changes.

For C++/CLI is the following right?:
ref class Packet{};

ref class Form1{
EndPoint^ remoteEP = safe_cast<EndPoint^>(gcnew IPEndPoint(IPAddress::Any, 0));
public: void test(remoteEP, [System::Runtime::InteropServices::Out]array<Byte>^ bydata){}
}

The remoteEP is an instance within same class so i just delete it, any problem?

David Anton
March 17th, 2008, 07:34 PM
That's not valid C# code (both parameters are just not compilable).
Start with valid C# code and run it through Instant C++.

POla
March 18th, 2008, 04:37 AM
That's not valid C# code (both parameters are just not compilable).
Start with valid C# code and run it through Instant C++.

Sry, the code i just simply type. The exact one i seen is like this:

For C#:
private void OnReceive(IAsyncResult ar){

EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0);
//Get the IP from where we got a message.
clientSocket.EndReceiveFrom(ar, ref receivedFromEP);
}

public static class ALawDecoder{
public static void ALawDecode(byte[] data, out short[] decoded){
int size = data.Length;
decoded = new short[size];
}
}

The code can run perfectly. Just concern the bold code, how should it be used in c++/cli?

David Anton
March 18th, 2008, 08:44 PM
private:
void OnReceive(IAsyncResult ^ar)
{
EndPoint ^receivedFromEP = gcnew IPEndPoint(IPAddress::Any, 0);
//Get the IP from where we got a message.
clientSocket::EndReceiveFrom(ar, receivedFromEP);
}

public ref class ALawDecoder sealed abstract
{
public:
static void ALawDecode(array<System::Byte> ^data, [System::Runtime::InteropServices::Out] array<short> ^%decoded)
{
int size = data->Length;
decoded = gcnew array<short>(size);
}
};

POla
March 26th, 2008, 01:46 AM
It is ok already, thank.