tags:

views:

98

answers:

3

I'm very much a vb person, but have had to use this id number class in c#. I got it from http://www.codingsanity.com/idnumber.htm :

using System;
using System.Text.RegularExpressions;

namespace Utilities.SouthAfrica
{
    /// <summary>
    /// Represents a South African Identity Number.
    /// valid number = 7707215230080
    /// invalid test number = 1234567891234 
   ///
    /// </summary>
    [Serializable()]
    public class IdentityNumber
    {

        #region Enumerations
        /// <summary>
        /// Indicates a gender.
        /// </summary>
        public enum PersonGender 
        {
            Female = 0,
            Male = 5
        }

        public enum PersonCitizenship 
        {
            SouthAfrican = 0,
            Foreign = 1
        }
        #endregion

        #region Declarations
        static Regex _expression;
        Match _match;
        const string _IDExpression = @"(?<Year>[0-9][0-9])(?<Month>([0][1-9])|([1][0-2]))(?<Day>([0-2][0-9])|([3][0-1]))(?<Gender>[0-9])(?<Series>[0-9]{3})(?<Citizenship>[0-9])(?<Uniform>[0-9])(?<Control>[0-9])";
        #endregion

        #region Constuctors
        /// <summary>
        /// Sets up the shared objects for ID validation.
        /// </summary>
        static IdentityNumber() 
        {
            _expression = new Regex(_IDExpression, RegexOptions.Compiled | RegexOptions.Singleline);
        }

        /// <summary>
        /// Creates the ID number from a string.
        /// </summary>
        /// <param name="IDNumber">The string ID number.</param>
        public IdentityNumber(string IDNumber)
        {
            _match = _expression.Match(IDNumber.Trim());
        }
        #endregion

        #region Properties
        /// <summary>
        /// Indicates the date of birth encoded in the ID Number.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown if the ID Number is not usable.</exception>
        public DateTime DateOfBirth 
        {
            get 
            {
                if(IsUsable == false) 
                {
                    throw new ArgumentException("ID Number is unusable!", "IDNumber");
                }
                int year = int.Parse(_match.Groups["Year"].Value);

                // NOTE: Do not optimize by moving these to static, otherwise the calculation may be incorrect
                // over year changes, especially century changes.
                int currentCentury = int.Parse(DateTime.Now.Year.ToString().Substring(0, 2) + "00");
                int lastCentury = currentCentury - 100;
                int currentYear = int.Parse(DateTime.Now.Year.ToString().Substring(2, 2));

                // If the year is after or at the current YY, then add last century to it, otherwise add
                // this century.
                // TODO: YY -> YYYY logic needs thinking about
                if(year > currentYear) 
                {
                    year += lastCentury;
                } 
                else 
                {
                    year += currentCentury;
                }
                return new DateTime(year, int.Parse(_match.Groups["Month"].Value), int.Parse(_match.Groups["Day"].Value));
            }
        }

        /// <summary>
        /// Indicates the gender for the ID number.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown if the ID Number is not usable.</exception>
        public PersonGender Gender 
        {
            get 
            {
                if(IsUsable == false) 
                {
                    throw new ArgumentException("ID Number is unusable!", "IDNumber");
                }
                int gender = int.Parse(_match.Groups["Gender"].Value);
                if(gender < (int) PersonGender.Male) 
                {
                    return PersonGender.Female;
                } 
                else 
                {
                     return PersonGender.Male;
                }
            }
        }

        /// <summary>
        /// Indicates the citizenship for the ID number.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown if the ID Number is not usable.</exception>
        public PersonCitizenship Citizenship 
        {
            get 
            {
                if(IsUsable == false) 
                {
                    throw new ArgumentException("ID Number is unusable!", "IDNumber");
                }
                return (PersonCitizenship) Enum.Parse(typeof(PersonCitizenship), _match.Groups["Citizenship"].Value);
            }
        }

        /// <summary>
        /// Indicates if the IDNumber is usable or not.
        /// </summary>
        public bool IsUsable 
        {
            get 
            {
                return _match.Success;
            }
        }

        /// <summary>
        /// Indicates if the IDNumber is valid or not.
        /// </summary>
        public bool IsValid
        {
            get 
            {
                if(IsUsable == true) 
                {
                    // Calculate total A by adding the figures in the odd positions i.e. the first, third, fifth,
                    // seventh, ninth and eleventh digits.
                    int a = int.Parse(_match.Value.Substring(0, 1)) + int.Parse(_match.Value.Substring(2, 1)) + int.Parse(_match.Value.Substring(4, 1)) + int.Parse(_match.Value.Substring(6, 1)) + int.Parse(_match.Value.Substring(8, 1)) + int.Parse(_match.Value.Substring(10, 1));

                    // Calculate total B by taking the even figures of the number as a whole number, and then
                    // multiplying that number by 2, and then add the individual figures together.
                    int b = int.Parse(_match.Value.Substring(1, 1) + _match.Value.Substring(3, 1) + _match.Value.Substring(5, 1) + _match.Value.Substring(7, 1) + _match.Value.Substring(9, 1) + _match.Value.Substring(11, 1));
                    b *= 2;
                    string bString = b.ToString();
                    b = 0;
                    for(int index = 0; index < bString.Length; index++) 
                    {
                        b += int.Parse(bString.Substring(index, 1));
                    }

                    // Calculate total C by adding total A to total B.
                    int c = a + b;

                    // The control-figure can now be determined by subtracting the ones in figure C from 10.
                    string cString = c.ToString() ;
                    cString = cString.Substring(cString.Length - 1, 1) ;
                    int control = 0;

                    // Where the total C is a multiple of 10, the control figure will be 0.
                    if(cString != "0") 
                    {
                        control = 10 - int.Parse(cString.Substring(cString.Length - 1, 1));
                    } 

                    if(_match.Groups["Control"].Value == control.ToString()) 
                    {
                        return true;
                    }
                }

                return false;
            }
        }
        #endregion

    }
}

Here is the code from my default.aspx.cs page:

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Utilities.Southafrica; <- this is the one i added to

    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {

    var number = new Utilities.SouthAfrica.IdentityNumber("7707215230080");
    Response.Write(number.Citizenship);
    Response.Write("<br />"); 
    Response.Write(number.DateOfBirth);
    Response.Write("<br />"); 
    Response.Write(number.IsValid);
    Response.Write("<br />"); 
    Response.Write(number.IsUsable);
    Response.Write("<br />"); 
    Response.Write(number.Gender);


        }
    }

Can someone please tell the syntax for how I pass an id number to the class?

Thanks

EDIT: I've updated the code with the correct working answer.

+3  A: 

You'll have to use the constructor.

var someNumber = new IdentityNumber("123456");

Then, you can use the properties of that class to find out the specifics of that Id number.

Console.WriteLine (someNumber.DateOfBirth);
Console.WriteLine (someNumber.Gender);
Console.WriteLine (someNumber.Citizenship);
Console.WriteLine (someNumber.IsValid);
Console.WriteLine (someNumber.IsUsable);
Frederik Gheysels
then i get the type or namespace identity number cannot be found are you missing a using directive or assembly reference. I have added the code from my default.aspx.cs page to the question so you can see. Thanks a lot
Phil
That's probably because that IdentityNumber class is in a different namespace then the namespace of the class in where you're using that class.So, add an additional 'using Utilities.SouthAfrica'.
Frederik Gheysels
Thanks for the help Frederik
Phil
+1  A: 
IdentityNumber number = new IdentityNumber("123456");
abatishchev
+1  A: 

All you need is to use provided Constructor like

 IdentityNumber someNumber = new IdentityNumber("006834"); 
Nasser Hadjloo