0

I'm trying to convert C++ code into C#. We have functions that accept pointers in C++. In C# we are having troubles doing it. We have tried the following demo code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test obj = new Test();
            int a,b;
            Console.WriteLine("Initial Values");
            a = 20;
            b = 100;
            obj.SetRef(ref a,ref b);
            Console.WriteLine("Value of A: " + a);
            Console.WriteLine("Value of B: " + b);
            obj.SetValRef(ref a);
            Console.WriteLine("After Ref");
            Console.WriteLine("Value of A: " + a);
            Console.WriteLine("Value of B: " + b);
            Console.ReadKey();
        }   
    }



    class Test
    {

        public void SetRef(ref int x, ref int y)
        {
            y = x;
        }

        public void SetValOut(out int x)
        {
            x = 10;

        }

        public void SetValRef(ref int x)
        {
            x = 10;

        }
    }
}

When we run it the output is

Initial Values
Value of A: 20
Value of B: 20
After Ref
Value of A: 10
Value of B: 20

We want that if value of one variable is changed then the value of second should automatically change( pointers).

3
  • First: Why not use a wrapper to give an C# interface around the C++ code? Second: Consider emitting a signal. Commented Oct 7, 2013 at 6:52
  • how wud a wrapper help? issue is only with the reference variables here.. Commented Oct 7, 2013 at 6:54
  • @NewtonSheikh Since he doesn't have to convert C++ Code to C# (though I don't know the reason) Commented Oct 7, 2013 at 6:54

2 Answers 2

4

The only way to do that in C#/.NET is to use unsafe code and to declare one of the variables as a pointer.

However, this is not good advice for C# code. I would highly consider restructuring the code to be more C#-like or you're going to fight the language a lot.

Or, a better advice, how about compiling the code with the managed C++ compiler and wrapping it up in some nice real managed types instead of going through the hassle of porting?

Anyway, here's a LINQPad example, however, that shows what to do if you really must port it and need that ability:

void Main()
{
    unsafe
    {
        int x = 10;
        int* y = &x;

        Debug.WriteLine("x=" + x + ", y=" + *y);

        ChangeValue(ref x);

        Debug.WriteLine("x=" + x + ", y=" + *y);

        ChangeValue(ref *y);

        Debug.WriteLine("x=" + x + ", y=" + *y);
    }
}

static void ChangeValue(ref int value)
{
    value += 10;
}

This will output:

x=10, y=10
x=20, y=20
x=30, y=30
Sign up to request clarification or add additional context in comments.

2 Comments

Then you must make sure you pass pointers around as well, or use reference passing. I've edited the code.
We have already ported all the code into c#. Now we stuck with few issues. And it is probably due to Pointer conversion. We will try to work with wrapper classes. Thanks for the help.
-1

You can use a 'pointer' on c#

using 'unsafe' keyword, and change compiler option to allow unsafe code

'ref' is not fit for it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.