Error when using dynamic in C #

Posted on

Question :

I made an application in C # and to integrate with Spring ERP and I passed the engine in VBA through the following line.

void IPublicMethods.UpdateCDU_IfItemSelected(ref dynamic doc, string artigo, int numLinha, ref dynamic plataformaPri, ref dynamic BSO)
{
    //Variable to tell if a document must set the CDUs for MoedaRef
    bool usaMoedaRef = false;

    try
    {
        //Get the value from DocumentosVanda table that tell if the current document must set the CDUs for MoedaRef
        plataformaPri.Dialogos.MostraAviso("Chegei!", Enums.PRI_Informativo, $"Cheguei antes do erro.");
        usaMoedaRef = BSO.Comercial.TabVendas.DaValorAtributo(doc.Tipodoc, "CDU_UsaMoedaRef");
     }
     catch (Exception ex)
     {
        plataformaPri.Dialogos.MostraAviso("Erro!", Enums.PRI_Informativo, $"Ocorreu um erro ao calcular os pre├žos {ex.Message}.");
     }        
}

And VBA is like this

    OpsMoedaRef.updateCDU_IfItemSelected Me.DocumentoVenda, artigo, numLinha, PlataformaPRIMAVERA, BSO

The error.

    

Answer :

Well the approach I follow is this:

Create an interface that allows you to expose in the COM world the properties you want to pass to .NET. Import are the metatags that are at the beginning ComVisible and [Guid("")] . These are the ones that will allow you to see this in the VBA.

Guid should generate a new one.

using System;
using System.Runtime.InteropServices;
using Interop.ErpBS900;

namespace Primavera.Extensibility
{
    [ComVisible(true)]
    [Guid("93C76FF4-D300-4BD9-911C-6A7BEBD648B2")]
    public interface IHostWindow
    {
        void AntesDeGravar(Boolean Cancel);

        ErpBS Motor { set; }

    }
}

Then you create a concrete class that implements this interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Interop.ErpBS900;

namespace Primavera.Extensibility
{
    [ComVisible(true)]
    [Guid("83F0D3D1-0B17-4453-852E-7CAA470B72AB")]
    [ClassInterface(ClassInterfaceType.None)]
    public class CustomerVBA : IHostWindow
    {
        #region internal

        internal ErpBS bso;

        #endregion     

        #region VBA Members

        public ErpBSMotor
        {
            set
            {
                bso = value;
            }

            get
            {
                return bso;
            }
        }

        public void AntesDeGravar(bool Cancel)
        {
            MessageBox.Show(BSO.Comercial.TabVendas.DaValorAtributo("FA", "descricao"));
        }

        #endregion
    }
}

Finally, more project properties mark this flag.

    

Leave a Reply

Your email address will not be published. Required fields are marked *