|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Comparing DefaultValue to DataRow column valuesbool rowChanged = false; foreach (DataColumn col in row.Columns) if (col != row.Table.PrimaryKey[0]) if (col.DefaultValue != row[col]) rowChanged = true; When debugging it I found that for a bool type column whose DefaultValue is true and whose row[col] value is true, the code goes into the last if statement and sets rowChanged to true. When I get VS.Net Command window to print out the value of (col.DefaultValue != row[col]) it even says false. However somewhat suspiciously the full dump of the variables col.DefaultValue and row[col] are different although they imply their values are the same. I seem to remember something about DataRow column values being held in types which allow for a DBNull.Value value. So I may be trying to compare two different types here I suspect. However I can't find any way of cooercing an object into another type where that type is held in a variable something like this: System.Type tp; object x = (tp)row[col]; I've had to write some hideous code which deals with all the different possible types on a case by case basis. Anyone got a better idea? PS anyone who writes a reply asking why I want to do this without suggesting any solution will get flamed James,
I don't know how you are debugging but the result of this loop will be in my opinion always be true. Cor Show quote "James" <jim***@hotmail.com> schreef in bericht news:1147043158.633592.249480@u72g2000cwu.googlegroups.com... > The code below should it seems to me work but doesnt: > > bool rowChanged = false; > foreach (DataColumn col in row.Columns) > if (col != row.Table.PrimaryKey[0]) > if (col.DefaultValue != row[col]) > rowChanged = true; > > When debugging it I found that for a bool type column whose > DefaultValue is true and whose row[col] value is true, the code goes > into the last if statement and sets rowChanged to true. When I get > VS.Net Command window to print out the value of (col.DefaultValue != > row[col]) it even says false. However somewhat suspiciously the full > dump of the variables col.DefaultValue and row[col] are different > although they imply their values are the same. > > I seem to remember something about DataRow column values being held in > types which allow for a DBNull.Value value. So I may be trying to > compare two different types here I suspect. > > However I can't find any way of cooercing an object into another type > where that type is held in a variable something like this: > > System.Type tp; > object x = (tp)row[col]; > > I've had to write some hideous code which deals with all the different > possible types on a case by case basis. > > Anyone got a better idea? > > PS anyone who writes a reply asking why I want to do this without > suggesting any solution will get flamed > Hi Cor
Thanks for replying. Why should this loop always be true? To simplify matters I have assumed there are no calculated columns in the table, however taking this into account would not make the loop always true. When debugging this means of comparison worked on most of the columns in the table including null columns and string-valued columns so demonstrably the loop would be false if the table had only the columns that I found compared correctly. James James,
Let say it in another way, when it is set to true it will never become false anymore. Cor Show quote "James" <jim***@hotmail.com> schreef in bericht news:1147104734.502606.289020@g10g2000cwb.googlegroups.com... > Hi Cor > > Thanks for replying. Why should this loop always be true? To simplify > matters I have assumed there are no calculated columns in the table, > however taking this into account would not make the loop always true. > When debugging this means of comparison worked on most of the columns > in the table including null columns and string-valued columns so > demonstrably the loop would be false if the table had only the columns > that I found compared correctly. > > James > Hi Cor
It seems you are unaware of standard C/C# syntax. if (test) action(); will only perform action() if test is true. Enclosing {} are unnecessary for a single statement. However the intention of the code should have been obvious. If you have any helpful comments they would be welcome. Conversely unhelpful comments will not. I think what you are running into is reference comparision vs. value
comparision. This example results in "false, true, true" object x = true, y = true; Console.WriteLine("{0} {1} {2}", x == y, x != y, x.Equals(y)); If you change your loop to, it should be closer to what you want. bool rowChanged = false; DataColumn pkey = row.Table.PrimaryKey[0]; foreach (DataColumn col in row.Columns) { if (col != pkey && !col.DefaultValue.Equals(row[col])) { rowChanged = true; break; } } // side note: DataTable.PrimaryKey returns a new DataColumn[] on every call. Show quote "James" wrote: > The code below should it seems to me work but doesnt: > > bool rowChanged = false; > foreach (DataColumn col in row.Columns) > if (col != row.Table.PrimaryKey[0]) > if (col.DefaultValue != row[col]) > rowChanged = true; > > When debugging it I found that for a bool type column whose > DefaultValue is true and whose row[col] value is true, the code goes > into the last if statement and sets rowChanged to true. When I get > VS.Net Command window to print out the value of (col.DefaultValue != > row[col]) it even says false. However somewhat suspiciously the full > dump of the variables col.DefaultValue and row[col] are different > although they imply their values are the same. > > I seem to remember something about DataRow column values being held in > types which allow for a DBNull.Value value. So I may be trying to > compare two different types here I suspect. > > However I can't find any way of cooercing an object into another type > where that type is held in a variable something like this: > > System.Type tp; > object x = (tp)row[col]; > > I've had to write some hideous code which deals with all the different > possible types on a case by case basis. > > Anyone got a better idea? > > PS anyone who writes a reply asking why I want to do this without > suggesting any solution will get flamed > > |
|||||||||||||||||||||||