Jump to content

Recommended Posts

Posted

Boas.

Soube que existe um código, que verifica se o NIF(Contribuinte) é válido.

Eu encontrei em VB 6, alguém me poderia converte-lo para C# 2010, sff?

Public Function IsValidContrib(ByVal contrib As String) As Boolean

     Dim s As String = contrib

     Dim c As Char

     Dim i, checkDigit As Integer

     If (s.Length = 9) Then

          c = s.Chars(0)

          If (c.Equals("1"c) Or c.Equals("2"c) Or c.Equals("5"c) Or c.Equals("6"c) Or c.Equals("8"c) Or c.Equals("9"c))Then

               checkDigit = Val(c) * 9

               For i = 2 To 8

                    checkDigit += Val(s.Chars(i - 1)) * (10 - i)

               Next

              checkDigit = 11 - (checkDigit Mod 11)

               If (checkDigit >= 10) Then checkDigit = 0

               If (checkDigit = Val(s.Chars(8))) Then Return True

          End If

     End If

     Return False

End Function
Posted
public bool IsValidContrib(string contrib)
{
string s= contrib
char c='';
int i=0;
int checkDigit=0;
if(s.Lenght==9)
           {
                   c= s[0];
                         if(c.Equals('1') || c.Equals('2') || c.Equals('5') || c.Equals('6') || c.Equals('8') || c.Equals('9'))
                          {
                               checkDigit=int.Parse(c) *9;
                               for(int i=2, i<8, i++)
                              {
                                checkDigit+= int.Parse(s[i-1])*(10-i)
                               }

                          checkDigit=11-(checkDigit%11)

                if (checkDigit >= 10)  checkDigit = 0

               if (checkDigit = int.Parse(s[8]) return true
            }
        } 
   return false

}

Fernando Lage Bastos - MCP/MCTS/MCPD

Posted

Já resolvi.

O código certo é:

       public bool IsValidContrib(string Contrib)
        {
            bool functionReturnValue = false;
            functionReturnValue = false;
            string[] s = new string[9];
            string Ss = null;
            string C = null;
            int i = 0;
            long checkDigit = 0;

            s[0] = Convert.ToString(Contrib[0]);
            s[1] = Convert.ToString(Contrib[1]);
            s[2] = Convert.ToString(Contrib[2]);
            s[3] = Convert.ToString(Contrib[3]);
            s[4] = Convert.ToString(Contrib[4]);
            s[5] = Convert.ToString(Contrib[5]);
            s[6] = Convert.ToString(Contrib[6]);
            s[7] = Convert.ToString(Contrib[7]);
            s[8] = Convert.ToString(Contrib[8]);

            if (Contrib.Length == 9)
            {
                C = s[0];
                if (s[0] == "1" || s[0] == "2" || s[0] == "5" || s[0] == "6" || s[0] == "9")
                {
                    checkDigit = Convert.ToInt32(C) * 9;
                    for (i = 2; i <= 8; i++)
                    {
                        checkDigit = checkDigit + (Convert.ToInt32(s[i - 1]) * (10 - i));
                    }
                    checkDigit = 11 - (checkDigit % 11);
                    if ((checkDigit >= 10))
                        checkDigit = 0;
                    Ss = s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] + s[7] + s[8];
                    if ((checkDigit == Convert.ToInt32(s[8])))
                        functionReturnValue = true;
                }
            }
            return functionReturnValue;
        }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.