public DataColumn this[int index] { get { if ((index < 0) || (index > _handler.Items)) { throw new IndexOutOfRangeException(); } else { return(_handler.Get(index)); } } set { if ((index < 0) || (index > _handler.Items)) { throw new IndexOutOfRangeException(); } else { _handler.Set(value, index); } } }
static void Main(string[] args) { // Test the column routines DataHandler dh = new DataHandler("", "test"); dh.Open(true); DataColumn c = new DataColumn(); c.ColumnName = "first"; c.DataType = System.Type.GetType("System.String"); c.MaxLength = 10; dh.Add(c); c = new DataColumn(); c.ColumnName = "second"; c.DataType = System.Type.GetType("System.String"); dh.Add(c); c = new DataColumn(); c.ColumnName = "third"; c.DataType = System.Type.GetType("System.Int16"); dh.Add(c); // Update - shorter string c = new DataColumn(); c.ColumnName = "2"; c.DataType = System.Type.GetType("System.String"); dh.Set(c, 1); // Update - longer string c = new DataColumn(); c.ColumnName = "deuxième"; c.DataType = System.Type.GetType("System.String"); dh.Set(c, 1); c = dh.Get(0); Console.WriteLine(c.ColumnName); // Remove - column c = new DataColumn(); c.ColumnName = "second"; dh.Remove(c); }
public object this[string columnName] { get { int columnIndex = -1; if (_table == null) { for (int index = 0; index < _handler.Items; index++) { if (_handler.Get(index).ColumnName == columnName) { columnIndex = index; break; } } } else { for (int index = 0; index < _table.Columns.Count; index++) { if (_table.Columns[index].ColumnName == columnName) { columnIndex = index; break; } } } if (columnIndex < 0) { throw new ArgumentException(); } else { return(_items[columnIndex]); } } set { int columnIndex = -1; if (_table == null) { for (int index = 0; index < _handler.Items; index++) { if (_handler.Get(index).ColumnName == columnName) { columnIndex = index; break; } } } else { for (int index = 0; index < _table.Columns.Count; index++) { if (_table.Columns[index].ColumnName == columnName) { columnIndex = index; break; } } } if (columnIndex < 0) { throw new ArgumentException(); } else { // The behavior seems to be to convert the data into the correct data type if (_table.Columns[columnIndex].DataType == value.GetType()) { // This is where we need to trap the string field length if (_table.Columns[columnIndex].DataType.GetType() == typeof(string)) { sbyte length = _table.Columns[columnIndex].MaxLength; if (length > 0) { string s = value.ToString().PadRight(length, '\0'); s = s.Substring(0, length); _items[columnIndex] = value; } else { _items[columnIndex] = value; } } else { _items[columnIndex] = value; } } else { try { if (_table.Columns[columnIndex].DataType.GetType() == typeof(string)) { sbyte length = _table.Columns[columnIndex].MaxLength; if (length > 0) { string s = value.ToString().PadRight(length, '\0'); s = s.Substring(0, length); _items[columnIndex] = value; } else { _items[columnIndex] = value; } } else { _items[columnIndex] = Convert.ChangeType(value, _table.Columns[columnIndex].DataType); } } catch { throw new FieldAccessException(); } } } } }