BPAY Ref Number (BPAY CRN) using C#

In this article, I am going to show how to create BPay Ref number (CRN) using c# based on the mod10 version 5 algorithm (MOD10V5).

protected void Page_Load(object sender, EventArgs e)
    {
       BPAYRefNumber bpay = new BPAYRefNumber();
 
       string BPAYYref = bpay.generateValidMOD10V5Number("45895624");
 
       Response.Write("MOD10V5 Number =" + BPAYYref);
       Response.Write("<br/>The number is valid =" + bpay.isNumberValidMOD10V5(BPAYYref));       
 
    }

public class BPAYRefNumber
{ 
    int checkDigit = 0;
    int digit = 0;
    Boolean result = false;
    String response = null;
    /**
     * Returns the checkbit for a number as per Luhn Mod 10 Version 5
     * 
     * @param number
     * @return
     */
    public int getCheckDigitMod10V5(String number)
    {
        checkDigit = 0;
        digit = 0; 
        try
        {   for (int i = 0; i < number.Length; i++)
            { 
                digit = int.Parse(number.Substring(i, 1));
                checkDigit += digit * (i + 1);
             }
             checkDigit = checkDigit % 10;
        }
        catch 
        { 
        }
        return checkDigit; 
    }
 
     /**
     * Checks if a number is valid per Luhn Mod 10 Version 5
     * 
     * @param number
     * @return
     */
    public Boolean isNumberValidMOD10V5(String number)
    { 
        try
        {
            result = ("" + getCheckDigitMod10V5(number.Substring(0, number.Length - 1))
                ).Equals(number.Substring(number.Length - 1, 1));
        }
        catch 
        { 
            result = false; 
        }
 
        return result;
    }
    /**
     * Generates a valid MOD10V5 number
     * 
     * @param number
     * @return
     */
    public String generateValidMOD10V5Number(String number)
    { 
        return number + this.getCheckDigitMod10V5(number); 
    }     
 
}