locked
Lightswitch VIN Custom Validation RRS feed

  • Question

  • I'm building some custom validation to verify Vehicle Identification Numbers (VINs) in a small LightSitch application. The custom validation is pretty simple for the most part, but I'm using code similar to what has been copied below from Rusty Davis' post at 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;
            }
        }

    The problem that I'm running into is that Visual Studio is telling me that "'System.Text.Encoding' does not contain a definition for 'ASCII'". It also says that the type or namespace "Hashtable" could not be found.

    Are these not available in LightSwitch projects? I've got all of the correct using statements. I'm confused.


    Tuesday, December 4, 2012 11:12 PM

Answers

  • Silverlight is unicode only no ASCII

    but you can use this function:

      public static byte[] StringToAscii(string s) {
            byte[] retval = new byte[s.Length];
            for (int ix = 0; ix < s.Length; ++ix) {
                char ch = s[ix];
                if (ch <= 0x7f) retval[ix] = (byte)ch;
                else retval[ix] = (byte)'?';
            }
            return retval;
        }
    And replace 
    Hashtable to List or something

    • Proposed as answer by LaurentzT Wednesday, December 19, 2012 9:13 PM
    • Marked as answer by Angie Xu Friday, January 4, 2013 5:41 AM
    Wednesday, December 19, 2012 9:13 PM