Using 'ref' Keyword to Create .NET DLL to be Called in LabVIEW

Updated May 3, 2018

Reported In

Software

  • LabVIEW Base

Issue Details

I am creating a class in C# to be called as a .NET DLL in LabVIEW. Must I use the 'ref' keyword in my function prototypes when calling a different class?

Solution

Using the 'ref' keyword will enable both an input and output tunnel in the Invoke Method function in LabVIEW. However, this does not mean that the class reference does not get returned otherwise. You can choose to omit this, and use the same reference for all subsequent calls.

Additional Information

Consider the following code snippet:
namespace TestDLL
{
	public class Class2	
	{
		public int number1 = 2;
		public int number2 = 4;
		public int number3 = 99;
	}

	public class Class1
	{
		public void square(ref int number)
		{
			number *= number;
		}

		public void add2(ref Class2 newclass)
		{
			newclass.number3 = newclass.number1 + newclass.number2;
		}

		public int add(int number 1, int number2)
		{
			return number1 + number2;
		}
	}
}
Using the ref keyword in the function prototype forces LabVIEW to provide both input and output nodes when calling the object method. As a result, the corresponding code for calling the .NET DLL in LabVIEW is a such.


The results are as follows, with the expected output.


Omitting the ref keyword with the following code snippet:
namespace TestDLL
{
	public class Class2	
	{
		public int number1 = 2;
		public int number2 = 4;
		public int number3 = 99;
	}

	public class Class1
	{
		public void square(ref int number)
		{
			number *= number;
		}

		public void add2(Class2 newclass)
		{
			newclass.number3 = newclass.number1 + newclass.number2;
		}

		public int add(int number 1, int number2)
		{
			return number1 + number2;
		}
	}
}
The corresponding code would be as such:


Running the program yields the same results.




 

WAS THIS ARTICLE HELPFUL?

Not Helpful