Main Content

Resolve Error Using .NET Base Class: Undefined Function or Variable

Issue

A frequently encountered error message when using a .NET class indicates that MATLAB® cannot find the specified class or variable:

Unrecognized function or variable 'ClassName'.

Possible Solutions

This happens because MATLAB cannot resolve the .NET type until the assembly is loaded using the NET.addAssembly function. Follow these steps to determine which assembly to load.

  1. Call dotnetenv in MATLAB. Note the Runtime and Version properties to determine the .NET Version.

  2. Go to the Microsoft® documentation for the class you want to use.

  3. Select the appropriate .NET Version, for example, Framework or .NET 9.

  4. The class definition on the reference page shows the assemblies containing the class. Call the NET.addAssembly function to load the assembly.

  5. Create or use the class in your MATLAB code.

See Also

|