<$BlogRSDUrl$>

Friday, March 19, 2004

What's wrong with this code?
MSDN: .NET Framework Class Library -> DataTable.PrimaryKey Property

private void SetPrimaryKeys(){
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable myTable = new DataTable();
DataColumn[] keys = new DataColumn[2];
DataColumn myColumn;
// Create column 1.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName= "FirstName";
// Add the column to the DataTable.Columns collection.
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[0] = myColumn;

// Create column 2 and add it to the array.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName = "LastName";
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[1] = myColumn;
// Set the PrimaryKeys property to the array.
myTable.PrimaryKey = keys;
}

As per the first link, the replies mostly complain about context, but lets dig a little.
myTable? myColum?
Why System.Type.GetType("System.String")? Why not typeof(System.String)?
Here is another example...

DataTable personTable = new DataTable(); // please why can't I just say DataTable person; as I would in C++
DataColumn firstName = new DataColumn(); // same again
firstName.ColumnName = "FirstName"; // why not use the constructor overloads?
firstName.DataType = typeof(System.String); // why would I call a function and make it look up a string?
DataColumn secondName = new DataColumn("SecondName", typeof(string) );
// damn could have used constructor overload from the start
personTable.PrimaryKeys = new DataColumn[] { firstName, secondName }; // Hello, arrays

So looks like the MSDN examples are really setting people up well...
Even better why not use first and second names for primary keys, identities are so last year?





Wednesday, March 17, 2004

Last weekend I ended up getting a call at 9.30pm on Saturday night, and as I was at the pub took pity on my manager and his need for a stable release for Monday.
Spent the whole of Sunday tracking down a fundamental flaw in the code, a nice side effect, a get that did a set...

Book of the week

This page is powered by Blogger. Isn't yours?