Thursday, December 19, 2013

A Dotnet Variable has not been Instantiated. Attempting to call - Dynamics NAV 2013/ NAV 2009



Hi,
A Dotnet Variable has not been Instantiated. Attempting to call, this error message will pop up when trying to call  a .Net Framework Assembly from CAL code.


when you calling a .Net Framework Assembly without constructor call  you will get above mentioned error message.

constructor is a method that creates an instance of an object and prepares it for use. Before you use a DotNet variable, you should verify whether the variable is static or is an object instance because this determines whether you have to use a constructor.
  • Static variables do not require a constructor because they do not have an instance.
  • Object instance variables require a constructor because they must be created before they can be accessed.
Example Code with constructor Call from CAL code

Asmbly := Asmbly.ConverttoUppercase();                          // Constructor Call
ConvsertyedString := Asmbly.ConverttoUp('marshal');   // Method Call
MESSAGE(ConvsertyedString);


Name DataType               Subtype
Asmbly DotNet                        UpperCase.ConverttoUppercase.'UpperCase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Marshal

How to call .Net Framework Assembly member from NAV2013 using CAL code / Extending Microsoft Dynamics NAV Using Microsoft .NET Framework Interoperability

Hi,
Below example will show how to call .Net framework Assembly member from NAV2013R2 using CAL code

1. open Visual Studio and create a class library application
2.Copy and paste below code in class library application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UpperCase
{
    public class ConverttoUppercase
    {
        public string customername = "";
        public ConverttoUppercase()
        {
        }
        public string ConverttoUp(String Custname)
        {
            customername = Custname.ToUpper();

            return customername;

        }
       
    }

}

3. Build  application (Build -> Build Solution), this will create a dll inside the bin folder of project folder
4. Copy the dll and past it inside "C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client\Add-ins"
5. Create a new code unit 
6. Create a global variable with data type as Dotnet and name it as UperCaseAsmbly
7. Click the assist edit button select recently created dll as sub type
8. Go to the properties of Dotnet variable and change RunOnClient property to yes
9. Create one more global variable with data type text and name it as ConvertedString
10. copy and paste below code in OnRun() of code unit


UperCaseAsmbly := UperCaseAsmbly.ConverttoUppercase(); // Constructor Call
ConvertedString := UperCaseAsmbly.ConverttoUp('marshal'); // Method Call
MESSAGE(ConvsertyedString);

11. Save and run code unit.

above example will convert lowercase letters to uppercase and show in message box.

dll which created in step 2 will have a method "ConverttoUp". This method will accept lower case letters and convert it into uppercase and return the result.

When calling .Net framework Assemblies in CAL code  first need to call the constructors for public variable, it is not needed for static variables


Thanks
Marshal