public object GetObject(int dwRow, int dwCol) { if (dwRow + 1 >= Array.GetLength(0)) { return(null); } if (dwCol >= Array [dwRow + 1].Length) { return(null); } string ob = Array [dwRow + 1] [dwCol]; if (ob != null) { object value; Type nValType = StringConverter.ConvertString(ob, out value); if (nValType == typeof(float) || nValType == typeof(double)) { float result = Convert.ToSingle(ob.ToString()); return(result); } if (nValType == typeof(byte) || nValType == typeof(short) || nValType == typeof(int)) { int result = Convert.ToInt32(ob.ToString()); return(result); } else { return(ob); } } return(null); }
public void TestCommonType() { object value1; object value2; Type typeA; Type typeB; typeA = StringConverter.ConvertString("\n", out value1); typeB = StringConverter.ConvertString("\n\n", out value2); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("x", out value1); typeB = StringConverter.ConvertString("xyz", out value2); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString(" ", out value1); typeB = StringConverter.ConvertString("\n", out value2); Assert.AreEqual(typeof(char), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(char), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("210", out value2); Assert.AreEqual(typeof(byte), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(byte), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("456", out value2); Assert.AreEqual(typeof(short), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(short), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("45678", out value2); Assert.AreEqual(typeof(int), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(int), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("456784567845678", out value2); Assert.AreEqual(typeof(long), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(long), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("18446744073709551615", out value2); Assert.AreEqual(typeof(ulong), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(ulong), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123", out value1); typeB = StringConverter.ConvertString("123.0", out value2); Assert.AreEqual(typeof(float), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(float), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("18446744073709551615", out value1); typeB = StringConverter.ConvertString("123.0", out value2); Assert.AreEqual(typeof(float), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(float), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("18446744073709551615184467440737095516151844674407370955161518446744073709551615", out value1); typeB = StringConverter.ConvertString("123.0", out value2); Assert.AreEqual(typeof(double), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(double), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("18446744073709551615184467440737095516151844674407370955161518446744073709551615", out value1); typeB = StringConverter.ConvertString("xyzabc", out value2); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("true", out value1); typeB = StringConverter.ConvertString("false", out value2); Assert.AreEqual(typeof(bool), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(bool), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("123.456E-48", out value1); typeB = StringConverter.ConvertString("false", out value2); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeB, typeA)); typeA = StringConverter.ConvertString("true", out value1); typeB = StringConverter.ConvertString("\n", out value2); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeA, typeB)); Assert.AreEqual(typeof(string), StringConverter.FindCommonType(typeB, typeA)); }
public void TestFindStringType() { object convertedValue; Assert.AreEqual(typeof(byte), StringConverter.ConvertString("153", out convertedValue)); Assert.IsTrue(convertedValue is byte); Assert.AreEqual(153, convertedValue); Assert.AreEqual(typeof(byte), StringConverter.ConvertString("0", out convertedValue)); Assert.IsTrue(convertedValue is byte); Assert.AreEqual(0, convertedValue); Assert.AreEqual(typeof(byte), StringConverter.ConvertString("255", out convertedValue)); Assert.IsTrue(convertedValue is byte); Assert.AreEqual(255, convertedValue); Assert.AreEqual(typeof(short), StringConverter.ConvertString("256", out convertedValue)); Assert.IsTrue(convertedValue is short); Assert.AreEqual(256, convertedValue); Assert.AreEqual(typeof(short), StringConverter.ConvertString("1573", out convertedValue)); Assert.IsTrue(convertedValue is short); Assert.AreEqual(1573, convertedValue); Assert.AreEqual(typeof(short), StringConverter.ConvertString("-36", out convertedValue)); Assert.IsTrue(convertedValue is short); Assert.AreEqual(-36, convertedValue); Assert.AreEqual(typeof(short), StringConverter.ConvertString("-32768", out convertedValue)); Assert.IsTrue(convertedValue is short); Assert.AreEqual(-32768, convertedValue); Assert.AreEqual(typeof(short), StringConverter.ConvertString("32767", out convertedValue)); Assert.IsTrue(convertedValue is short); Assert.AreEqual(32767, convertedValue); Assert.AreEqual(typeof(int), StringConverter.ConvertString("-32769", out convertedValue)); Assert.IsTrue(convertedValue is int); Assert.AreEqual(-32769, convertedValue); Assert.AreEqual(typeof(int), StringConverter.ConvertString("5132763", out convertedValue)); Assert.IsTrue(convertedValue is int); Assert.AreEqual(5132763, convertedValue); Assert.AreEqual(typeof(int), StringConverter.ConvertString("2147483647", out convertedValue)); Assert.IsTrue(convertedValue is int); Assert.AreEqual(2147483647, convertedValue); Assert.AreEqual(typeof(int), StringConverter.ConvertString("-2147483648", out convertedValue)); Assert.IsTrue(convertedValue is int); Assert.AreEqual(-2147483648, convertedValue); Assert.AreEqual(typeof(long), StringConverter.ConvertString("2147483648", out convertedValue)); Assert.IsTrue(convertedValue is long); Assert.AreEqual(2147483648, convertedValue); Assert.AreEqual(typeof(long), StringConverter.ConvertString("-2147483649", out convertedValue)); Assert.IsTrue(convertedValue is long); Assert.AreEqual(-2147483649, convertedValue); Assert.AreEqual(typeof(long), StringConverter.ConvertString("9223372036854775807", out convertedValue)); Assert.IsTrue(convertedValue is long); Assert.AreEqual(9223372036854775807, convertedValue); Assert.AreEqual(typeof(ulong), StringConverter.ConvertString("9223372036854775808", out convertedValue)); Assert.IsTrue(convertedValue is ulong); Assert.AreEqual(9223372036854775808, convertedValue); Assert.AreEqual(typeof(ulong), StringConverter.ConvertString("18446744073709551615", out convertedValue)); Assert.IsTrue(convertedValue is ulong); Assert.AreEqual(18446744073709551615, convertedValue); Assert.AreEqual(typeof(float), StringConverter.ConvertString("18446744073709551616", out convertedValue)); Assert.IsTrue(convertedValue is float); Assert.AreEqual(18446744073709551616F, convertedValue); Assert.AreEqual(typeof(float), StringConverter.ConvertString("2E16", out convertedValue)); Assert.IsTrue(convertedValue is float); Assert.AreEqual(2E16F, convertedValue); Assert.AreEqual(typeof(float), StringConverter.ConvertString("1.7634E-16", out convertedValue)); Assert.IsTrue(convertedValue is float); Assert.AreEqual(1.7634E-16F, convertedValue); Assert.AreEqual(typeof(float), StringConverter.ConvertString("-1.5E-45", out convertedValue)); Assert.IsTrue(convertedValue is float); Assert.AreEqual(-1.5E-45F, convertedValue); Assert.AreEqual(typeof(float), StringConverter.ConvertString("1.5E-45", out convertedValue)); Assert.IsTrue(convertedValue is float); Assert.AreEqual(1.5E-45F, convertedValue); // Not sure how to find a number that will get parsed by float.TryParse() but not double.TryParse() // which means FindStringType() may never actually return typeof(double) Assert.AreEqual(typeof(bool), StringConverter.ConvertString("true", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsTrue((bool)convertedValue); Assert.AreEqual(typeof(bool), StringConverter.ConvertString("TRUE", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsTrue((bool)convertedValue); Assert.AreEqual(typeof(bool), StringConverter.ConvertString("True", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsTrue((bool)convertedValue); Assert.AreEqual(typeof(bool), StringConverter.ConvertString("false", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsFalse((bool)convertedValue); Assert.AreEqual(typeof(bool), StringConverter.ConvertString("False", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsFalse((bool)convertedValue); Assert.AreEqual(typeof(bool), StringConverter.ConvertString("FALSE", out convertedValue)); Assert.IsTrue(convertedValue is bool); Assert.IsFalse((bool)convertedValue); Assert.AreEqual(typeof(char), StringConverter.ConvertString("x", out convertedValue)); Assert.IsTrue(convertedValue is char); Assert.AreEqual('x', convertedValue); Assert.AreEqual(typeof(char), StringConverter.ConvertString("\t", out convertedValue)); Assert.IsTrue(convertedValue is char); Assert.AreEqual('\t', convertedValue); Assert.AreEqual(typeof(char), StringConverter.ConvertString(" ", out convertedValue)); Assert.IsTrue(convertedValue is char); Assert.AreEqual(' ', convertedValue); Assert.AreEqual(typeof(char), StringConverter.ConvertString("\n", out convertedValue)); Assert.IsTrue(convertedValue is char); Assert.AreEqual('\n', convertedValue); Assert.AreEqual(typeof(string), StringConverter.ConvertString("\n\n", out convertedValue)); Assert.IsTrue(convertedValue is string); Assert.AreEqual("\n\n", convertedValue); Assert.AreEqual(typeof(string), StringConverter.ConvertString("wxyz", out convertedValue)); Assert.IsTrue(convertedValue is string); Assert.AreEqual("wxyz", convertedValue); Assert.AreEqual(typeof(string), StringConverter.ConvertString("$3.85", out convertedValue)); Assert.IsTrue(convertedValue is string); Assert.AreEqual("$3.85", convertedValue); Assert.AreEqual(typeof(string), StringConverter.ConvertString("", out convertedValue)); Assert.IsTrue(convertedValue is string); Assert.AreEqual("", convertedValue); }
/// <summary> /// Read the next object from the currentLine string /// </summary> /// <returns>The next object in the currentLine string</returns> private object ReadNextObject() { if (currentLine == null) { return(null); } // Check to see if the next value is quoted bool quoted = false; if (currentLine.StartsWith("\"")) { quoted = true; } // Find the end of the next value string nextObjectString = ""; int i = 0; int len = currentLine.Length; bool foundEnd = false; while (!foundEnd && i <= len) { // Check if we've hit the end of the string if ((!quoted && i == len) || // non-quoted strings end with a comma or end of line (!quoted && currentLine.Substring(i, 1) == TAB) // quoted strings end with a quote followed by a comma or end of line || (quoted && i == len - 1 && currentLine.EndsWith("\"")) || (quoted && currentLine.Substring(i, 2) == TAB_QUOTE)) { foundEnd = true; } else { i++; } } if (quoted) { if (i > len || !currentLine.Substring(i, 1).StartsWith("\"")) { throw new FormatException("Invalid Table file format: " + currentLine.Substring(0, i)); } i++; } nextObjectString = currentLine.Substring(0, i).Replace("\"\"", "\""); if (i < len) { currentLine = currentLine.Substring(i + 1); } else { currentLine = ""; } if (quoted) { if (nextObjectString.StartsWith("\"")) { nextObjectString = nextObjectString.Substring(1); } if (nextObjectString.EndsWith("\"")) { nextObjectString = nextObjectString.Substring(0, nextObjectString.Length - 1); } return(nextObjectString); } else { object convertedValue; StringConverter.ConvertString(nextObjectString, out convertedValue); return(convertedValue); } }