Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

I need help on passing this c# code to x++ code

(0) ShareShare
ReportReport
Posted on by 148

Hi, 

I'm a newbie in x and I was trying to write this c# code (sorry for the double Static void but for some reason it won't let me edit):

static void Main(string[] args)
static void Main(string[] args)
{
    Response response = ReadSerie();
    Serie serie = response.seriesResponse.series[0];
    Console.WriteLine("Serie: {0}", serie.Title);
    foreach (DataSerie dataSerie in serie.Data)
    {
        if (dataSerie.Data.Equals("N/E")) continue;
        Console.WriteLine("Fecha: {0}", dataSerie.Date);
        Console.WriteLine("Dato: {0}", dataSerie.Data);
    }
    Console.ReadLine();
}

I did try in this way but I have errors in every strFmt saying "The name 'Exception' does not denote a class, a table, or an extended data type. " and the foreach the syntax is not correct it seems: 

using BanxicoDll;
using System;
using System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization; 




class BanxicoConsumo
{
    static void Main()
    {
        ;
        BanxicoDll.DataSerie dataSerie;
        str url = "https://www.banxico.org.mx/SieAPIRest/service/v1/series/SF61745/datos/2023-01-31/2023-01-31";
        // Crea una instancia de la clase Program de la DLL BanxicoDll
        Program banxico = new Program();
        try
        {
            // Utiliza el método ReadSerie de la instancia de Program para obtener la respuesta del API
            Response response = BanxicoDll.Program::ReadSerie(url);

            // Si la respuesta no es nula, procesa la serie
            if (response != null && response.seriesResponse.series.Length > 0)
            {
                BanxicoDll.Serie serie = response.seriesResponse.series.GetValue(0);
                info(strFmt("Serie: %1", serie.Title));
                for (BanxicoDll.DataSerie dataSerie : serie.Data)
                {
                    if (dataSerie.Data == "N/E") continue;
                    info(strFmt("Fecha: %1", dataSerie.Date));
                    info(strFmt("Dato: %1", dataSerie.Data));
                }
            }
            else
            {
                info(strFmt("No se pudo obtener la serie del API de Banxico."));
            }
        }
        catch (Exception::CLRError)
        {
            info("Se produjo un error al conectarse con el API de Banxico.");
        }
        

    }

}

  • Daniel Mora Profile Picture
    Daniel Mora 148 on at
    RE: I need help on passing this c# code to x++ code

    Thank you so much Martin, you are great.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: I need help on passing this c# code to x++ code

    Ah, sorry. The namespace is System.Collections, not just System.

  • Daniel Mora Profile Picture
    Daniel Mora 148 on at
    RE: I need help on passing this c# code to x++ code

    Yes, sorry, I copied the wrong code. In regards to your suggestion, it says that "The name 'System.IEnumerable' does not denote a class, a table, or an extended data type." Do I have to update any model or declare any library?

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: I need help on passing this c# code to x++ code

    I'm not familiar with this syntax

    for (BanxicoDll.DataSerie dataSerie : serie.Data)

    and you don't seem to have in your C# code. This is whay you're using:

    foreach (DataSerie dataSerie in serie.Data)
    {
        ...
    }

    This is basically a shortcut for using an enumerator - foreach calls GetEnumerator() method of serie.Data and iterates the elements. X don't have foreach, but you can do the same thing as foreach. For example:

    System.IEnumerable series = serie.Data;
    System.Collections.IEnumerator enumerator = series.GetEnumerator();
    while (enumerator.MoveNext())
    {
    	DataSerie serie = enumerator.Current;
    	...
    }

  • Daniel Mora Profile Picture
    Daniel Mora 148 on at
    RE: I need help on passing this c# code to x++ code

    That worked! Thanks Martin. As for the "for" statement how would it work? I'm checking the documentation and the syntax in x is "for ( initialization ; test ; increment ) { statement }" but I don't know how would it apply if I have it in c# as:

    for (BanxicoDll.DataSerie dataSerie : serie.Data)
                    {
                        if (dataSerie.Data == "N/E") continue;
                        info(strFmt("Fecha: %1", dataSerie.Date));
                        info(strFmt("Dato: %1", dataSerie.Data));
                    }


    Thanks in advance

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: I need help on passing this c# code to x++ code

    Throw away all those 'using' statements that you don't need, especially 'using System'. This is causing a name conflict; the compiler doesn't know whether Exception means the F&O enum or System.Exception class.

  • Daniel Mora Profile Picture
    Daniel Mora 148 on at
    RE: I need help on passing this c# code to x++ code

    Hi André, I'm checking this enum "Exception" in the AOT and it appears under System Documentation - Base Enums - Exception. But it won't show to which model it belongs as it does for the other base enums under Data Types:2806.Exception.png

    Also, the exact error is not highlighting the line with catch. Is highlighting every line that starts with info. Thanks in advance.

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 292,160 Super User 2025 Season 1 on at
    RE: I need help on passing this c# code to x++ code

    Hi Daniel,

    I guess the exact error is highlighting the line with catch (Exception::CLRError)? In that case, check which model references you have set for your custom model. The model containing the Exception enumeration might not be referenced yet.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,160 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans