converts from Vb to c#

Asked

Viewed 92 times

-1

I have this code in Vb, I need it in c#. In Vb create a new code each time it is executed, in c# always puts the same code "000011".

Public Function CalculaNovoCodBarras() As String

Dim lista As New StdBELista
Dim res As String
Dim flag As Boolean
Dim MaxArt As String
MaxArt = "00001"
flag = False
On Error GoTo erro

Do
    Set lista = BSO.Consulta("Select substring(Artigo.codbarras,8,5) as MaxArtigo FROM ARTIGO where substring(Artigo.codbarras,8,5) = '" & MaxArt & "'")
    If lista.NoFim Then
        res = GetCodPaisEmpresa & MaxArt
        res = res & CalculaDigitoEAN13(res)
        flag = True
    Else
        MaxArt = Format(MaxArt + 1, "00000")
    End If
Loop While flag = False
CalculaNovoCodBarras = res

 Exit Function
 erro:
 MsgBox Err.Description
End Function

C#

public string CalculaNovoCodBarras()
    {
        StdBELista lista = new StdBELista();
        string res = "";
        bool flag = false;
        string MaxArt = "00001";
        //  MaxArt = "00001";
        //flag = false;

        do
        {
            lista = BSO.Consulta("Select substring(Artigo.codbarras,8,5) as MaxArtigo FROM ARTIGO where substring(Artigo.codbarras,8,5) = '" + MaxArt + "'");
            if (lista.NoFim())
            {
                res = GetCodPaisEmpresa() + MaxArt;
                res = res + CalculaDigitoEAN13();
                flag = true;
            }

            else
                MaxArt = String.Format(MaxArt + 1, "00000");
        }
        while (flag == false);

        return res;
    }
  • 1

    Always report the error and which line the error occurs :)

  • Not giving error , in Vb is generating a new code, sequential with 5 digit format each time it is running in c# this giving the same code with 6 digits.

  • What is your question? You want to increase MaxArt and fill in with 0

2 answers

1


If you want to increment the value that is coming as "000001" and fill in with zeros again, you must convert the value to integer and use the PadLeft() for the completion. If you try to carry out the sum with a string and an integer, the operation will not be performed and the result will be a simple concatenation.

string MaxArt = "00001";
//MaxArt == "00002"
MaxArt = (int.Parse(MaxArt) + 1).ToString().PadLeft(5, '0');
  • I want to increment Maxart but always have the 5-digit format

  • Then do the Pad with 5 jokers... But this will work until you arrive in 99999

  • @Was that your question? You saw the issue in the answer?

  • Yes, thank you.

  • So, you should mark this one as "accept answer" and delete your other because it is not a real answer and doesn’t have much relevance to another user facing a similar problem in the future.

0

The final code went like this

    public string CalculaNovoCodBarras()
    {
        StdBELista lista = new StdBELista();
        string res = "";
        bool flag;
        string MaxArt = "00001";
        flag = true;

        do
        {
            lista = BSO.Consulta("Select substring(Artigo.codbarras,8,5) as MaxArtigo FROM ARTIGO where substring(Artigo.codbarras,8,5)= '" + MaxArt + "'");
            if (lista.NoFim())
            {

                res = GetCodPaisEmpresa() + MaxArt;
                res = res + CalculaDigitoEAN13(res);
                flag = false;
                break;
            }

            else

                MaxArt = (int.Parse(MaxArt) + 1).ToString().PadLeft(5, '0');


            flag = true;
            continue;
        }
        while (flag);

        return res;
    }

Browser other questions tagged

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