Main Content

Call .NET Methods With ref Keyword

This example shows how to call methods that use a ref keyword in the argument list.

The input argument db1 in the following refTest method is modified by the ref keyword.

using System;
namespace netdoc
{
    public class SampleRefTest
    {
        //test ref keyword
        public void refTest(ref double db1)
        {
            db1 = db1 * 2;
        }
    }
}

The function signature in MATLAB® is:

Return TypeNameArguments
double scalar db1refTest(netdoc.SampleRefTest this,
double scalar db1)

Create an assembly from the SampleRefTest code, using instructions in Build a .NET Application for MATLAB Examples.

Create an asmpath variable set to the full path to the DLL file, SampleRefTest.dll, created by your development tool. For example:

asmpath = 'c:\work\Visual Studio 2012\Projects\SampleRefTest\SampleRefTest\bin\Debug\';
asmname = 'SampleRefTest.dll';

Load the assembly.

asm = NET.addAssembly(fullfile(asmpath,asmname));

Call the method.

cls = netdoc.SampleRefTest;
db4 = refTest(cls,6)
db4 =
    12

Related Examples

More About