2010-04-14 60 views
2

我是一个vb人,但不得不在c#中使用这个id号码类。我是从http://www.codingsanity.com/idnumber.htm如何传递一个id号字符串到这个类

namespace Utilities 
{ 
    [Serializable] 
    public class IdentityNumber 
    { 
     public enum PersonGender 
     { 
      Female = 0, 
      Male = 5 
     } 

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

     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])"; 

     static IdentityNumber() 
     { 
      _expression = new Regex(_IDExpression, RegexOptions.Compiled | RegexOptions.Singleline); 
     } 

     public IdentityNumber(string IDNumber) 
     { 
      _match = _expression.Match(IDNumber.Trim()); 
     } 

     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)); 
      } 
     } 

     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; 
       } 
      } 
     } 

     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; 
      } 
     } 
    } 
} 

能有人告诉了我的语法如何通过一个ID号到类?

+1

顺便说一句,你可以像'如果(!IsUsable){}' – abatishchev 2010-04-14 07:07:33

+1

DAH如果-子句中使用布尔...我无法应付'(fooBoolean == true/false)':) – 2010-04-14 07:09:30

回答

4

您必须使用构造函数。

var someNumber = new IdentityNumber("123456"); 

然后,您可以使用该类的属性来找出该Id编号的具体内容。

Console.WriteLine (someNumber.DateOfBirth); 
Console.WriteLine (someNumber.Gender); 
Console.WriteLine (someNumber.Citizenship); 
Console.WriteLine (someNumber.IsValid); 
Console.WriteLine (someNumber.IsUsable); 
+0

然后我得到的类型或名称空间身份号码无法找到是你缺少一个使用指令或程序集引用。我已将default.aspx.cs页面中的代码添加到问题中,以供您查看。非常感谢 – Phil 2010-04-14 07:11:55

+0

这可能是因为IdentityNumber类位于不同的命名空间中,因此您使用该类的类的名称空间不同。 因此,添加一个'使用Utilities.SouthAfrica'。 – 2010-04-14 07:41:39

+0

感谢弗雷德里克的帮助 – Phil 2010-04-14 08:44:04

0
IdentityNumber number = new IdentityNumber("123456"); 
0

所有你需要的是使用提供的构造像

IdentityNumber someNumber = new IdentityNumber("006834"); 
相关问题