0

I need to write an interface in Visual C++, what will be used in C# project. I need to use out parameter in C#, how will i make C++ signature?

C++ code like

public interface class Iface
{
public:
    System::Object^ Method([out] bool there);
};

and C# must be

public class TestObj : Library.Iface
    {
        object Library.Iface.Method(out bool there)
        {
            there = true;
            return null;
        }
    }

How will i write my C++ interface?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    I believe that C++/CLI only gives you normal and `ref` parameters (when you use the `%` managed reference sigil). – Rook Jun 20 '12 at 13:15
  • Why do you want to use an `out` why exactly is wrong with your code? One problem I see is you declared it as an interface class something tells me that isn't correct. – Security Hound Jun 20 '12 at 13:23
  • Thank you. I have not many experence in C++/CLI – user1469310 Jun 20 '12 at 13:23
  • Some related questions: http://stackoverflow.com/questions/3514237/ref-and-out-in-c-cli http://stackoverflow.com/questions/186477/in-c-cli-how-do-i-declare-and-call-a-function-with-an-out-parameter – Matt Smith Jun 20 '12 at 14:48

1 Answers1

3
using namespace System;
using namespace System::Runtime::InteropServices;
interface class Iface
{
    Object^ Method([Out] bool% there);
};
IngisKahn
  • 859
  • 6
  • 11
  • @Hans: Wrong. It affects the C# compiler dataflow analysis. Without the `[Out]`, the C# compiler will demand that the argument is definitely assigned before the call. With the `[Out]`, the C# compiler will require the implementing method to definitely assign the parameter before returning. – Ben Voigt Jun 20 '12 at 13:29
  • Hmm, no, C# does in fact use *out* for the implementation method. This answer is correct, +1. – Hans Passant Jun 20 '12 at 13:35