Error using Dynamic in C#

Asked

Viewed 195 times

-1

I made an application in C# and to integrate with ERP Spring and 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 the VBA is like this

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

The mistake. inserir a descrição da imagem aqui

  • Where exactly does he make the mistake? Have you put any breakpoint to figure out where it is?

  • And do you really need to do this? It seems like you know what kind you’re going to get. Anyway, he doesn’t seem to be responsible for the mistake. Catching an exception like this is horrible in C#, I don’t know if this ERP forces you to do this atrocity.

  • @acamiloMoz: Don’t forget to mark the answers that help you validate :)

  • João Martins The error occurs in the next line usaMoedaRef = BSO.Comercial.Tabvendas.Davaloratributo(doc.Tipodoc, "Cdu_usamoedaref"); because at runtime is not recognized Tabvendas as property or method of the BSO object.Comercial.

  • Maybe the engine Commercial is not properly instantiated. If you follow @Sérgiosereno’s instructions you cannot have it functional?

  • Good, VBA does not work well with Dynamic, but if you use Object instead of Dynamic the popup well

  • @Davidferreira Ué, and I think the dynamic had been mainly created to interoperate with VBA and things like.

Show 2 more comments

1 answer

0


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("")]. They are the ones that will allow you to see this in VBA.

Guid must generate a new.

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 interaction:

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 but project properties mark this flag.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Good Sereno I am following the same approach what is happening is that the Tabvendas property is not recognized as a member of the commercial. BSO.Comercial.Tabvendas.Davaloratributo

  • If you follow the example I gave you doesn’t work? I have it working like this.

  • I was now creating a small project to simulate and it still works. the error 'System. _Comobject' does not contain a Definition for 'Tabvendas'.

  • 1

    Well the problem is in Dynamic. Use object references and everything will work. I updated the above example

  • With the interops fucked well.

  • Now I’m having another challenge. I am updating the unit price of lines in the Sales edits and would like to update the field with a negative number and it is only assumed to be positive. ex: Item.Precounit = -1*20 the result is always shown on line 20.

  • @acamiloMoz Raises another question with this doubt. Every question in this forum should be only of a specific problem.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.