【问题标题】:Lightswitch VIN Custom Validation灯开关 VIN 自定义验证
【发布时间】:2012-12-04 23:10:32
【问题描述】:

我正在构建一些自定义验证来验证小型 LightSitch 应用程序中的车辆识别码 (VIN)。在大多数情况下,自定义验证非常简单,但我使用的代码类似于下面从 Rusty Davis 的帖子http://shapemetrics.wordpress.com/2010/08/23/vin-validation/ 复制的代码。

public class VIN
    {
        //Make sure no instance of this class is created... only method is static. 
        private VIN() { }

        public static bool IsValidVin(string p_strVin)
        {
            bool blnIsValid = false;
            int intValue = 0;
            int[] intWeights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 };

            if (p_strVin == null)
            {
                return false;
            }
            else if (p_strVin.Length != 17)
            {
                return blnIsValid;
            }

            p_strVin = p_strVin.ToUpper().Trim();
            int intCheckValue = 0;
            char check = p_strVin[8];
            char year = p_strVin[9];

            if (!char.IsDigit(check) && check != 'X')
            {
                return blnIsValid;
            }
            else
            {
                if (check != 'X')
                {
                    char[] d = new char[] { check };
                    intCheckValue = int.Parse(Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(d)));
                }
                else
                {
                    intCheckValue = 10;
                }
            }

            Hashtable replaceValues = new Hashtable();
            replaceValues.Add('A', 1);
            replaceValues.Add('B', 2);
            replaceValues.Add('C', 3);
            replaceValues.Add('D', 4);
            replaceValues.Add('E', 5);
            replaceValues.Add('F', 6);
            replaceValues.Add('G', 7);
            replaceValues.Add('H', 8);
            replaceValues.Add('J', 1);
            replaceValues.Add('K', 2);
            replaceValues.Add('L', 3);
            replaceValues.Add('M', 4);
            replaceValues.Add('N', 5);
            replaceValues.Add('P', 7);
            replaceValues.Add('R', 9);
            replaceValues.Add('S', 2);
            replaceValues.Add('T', 3);
            replaceValues.Add('U', 4);
            replaceValues.Add('V', 5);
            replaceValues.Add('W', 6);
            replaceValues.Add('X', 7);
            replaceValues.Add('Y', 8);
            replaceValues.Add('Z', 9);
            replaceValues.Add('1', 1);
            replaceValues.Add('2', 2);
            replaceValues.Add('3', 3);
            replaceValues.Add('4', 4);
            replaceValues.Add('5', 5);
            replaceValues.Add('6', 6);
            replaceValues.Add('7', 7);
            replaceValues.Add('8', 8);
            replaceValues.Add('9', 9);
            replaceValues.Add('0', 0);

            //Make sure it is a Valid Year 
            if (!replaceValues.Contains(year) && year != '0')
            {
                return blnIsValid;
            }

            //Make sure characters that are in the VIN are the ones allowed. 
            for (int i = 0; i < p_strVin.Length; i++)
            {
                if (!replaceValues.Contains(p_strVin[i]))
                {
                    return false;
                }
                intValue += (intWeights[i] * ((int)replaceValues[p_strVin[i]]));
            }

            if ((intValue % 11) == intCheckValue)
            {
                blnIsValid = true;
            }

            return blnIsValid;
        }
    }

我遇到的问题是 Visual Studio 告诉我“'System.Text.Encoding' 不包含 'ASCII' 的定义”。它还说找不到类型或命名空间“Hashtable”。

这些在 LightSwitch 项目中不可用吗?我有所有正确的 using 语句。我很困惑。

【问题讨论】:

    标签: c# silverlight visual-studio-lightswitch


    【解决方案1】:

    由于Lightswitch是Silverlight应用程序,所以不支持ascii编码,see this link.建议您尝试改用Encoding.UTF8

    至于 Hashtable,Silverlight 也不支持它。另一种方法是改用Dictionary &lt;Tkey, TValue&gt;See this link.

    更新后的代码如下所示(未经测试的代码):

            bool blnIsValid = false;
            int intValue = 0;
            int[] intWeights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 };
    
            if (p_strVin == null)
            {
                return false;
            }
            else if (p_strVin.Length != 17)
            {
                return blnIsValid;
            }
    
            p_strVin = p_strVin.ToUpper().Trim();
            int intCheckValue = 0;
            char check = p_strVin[8];
            char year = p_strVin[9];
    
            if (!char.IsDigit(check) && check != 'X')
            {
                return blnIsValid;
            }
            else
            {
                if (check != 'X')
                {
                    char[] d = new char[] { check };
                    var bytes = Encoding.UTF8.GetBytes(d);
                    intCheckValue = int.Parse(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
                }
                else
                {
                    intCheckValue = 10;
                }
            }
    
            Dictionary<char, int> replaceValues = new Dictionary<char, int>();
            replaceValues.Add('A', 1);
            replaceValues.Add('B', 2);
            replaceValues.Add('C', 3);
            replaceValues.Add('D', 4);
            replaceValues.Add('E', 5);
            replaceValues.Add('F', 6);
            replaceValues.Add('G', 7);
            replaceValues.Add('H', 8);
            replaceValues.Add('J', 1);
            replaceValues.Add('K', 2);
            replaceValues.Add('L', 3);
            replaceValues.Add('M', 4);
            replaceValues.Add('N', 5);
            replaceValues.Add('P', 7);
            replaceValues.Add('R', 9);
            replaceValues.Add('S', 2);
            replaceValues.Add('T', 3);
            replaceValues.Add('U', 4);
            replaceValues.Add('V', 5);
            replaceValues.Add('W', 6);
            replaceValues.Add('X', 7);
            replaceValues.Add('Y', 8);
            replaceValues.Add('Z', 9);
            replaceValues.Add('1', 1);
            replaceValues.Add('2', 2);
            replaceValues.Add('3', 3);
            replaceValues.Add('4', 4);
            replaceValues.Add('5', 5);
            replaceValues.Add('6', 6);
            replaceValues.Add('7', 7);
            replaceValues.Add('8', 8);
            replaceValues.Add('9', 9);
            replaceValues.Add('0', 0);
    
            //Make sure it is a Valid Year 
            if (!replaceValues.ContainsKey(year) && year != '0')
            {
                return blnIsValid;
            }
    
            //Make sure characters that are in the VIN are the ones allowed. 
            for (int i = 0; i < p_strVin.Length; i++)
            {
                if (!replaceValues.ContainsKey(p_strVin[i]))
                {
                    return false;
                }
                intValue += (intWeights[i] * ((int)replaceValues[p_strVin[i]]));
            }
    
            if ((intValue % 11) == intCheckValue)
            {
                blnIsValid = true;
            }
    
            return blnIsValid;
    

    【讨论】:

      【解决方案2】:

      您可能还想查看更新版本的代码。你在那里的那个给出了一个基本的布尔答案。

      相反,网站上还有另一个反映更详细信息的信息:http://shapemetrics.wordpress.com/2011/12/05/vin-validate-improved

      【讨论】:

      • 感谢生锈。我做了一些改变,给出了布尔响应。
      • 我创建了一个网络服务,如果有人感兴趣的话。我正在努力将 make/model 添加到数据库中。但是,它能够提供更详细的信息,就像上面的链接一样,还有一些补充。 shapemetrics.wordpress.com/2013/08/16/…
      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 2013-12-12
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多