List classList = new List();
-
Hey guys, I'm trying to check the uniqueness of the index key of visual foxpro tables. From the tableinfo table I read all names of the tables in the database and the name of their index key. The name, index key and path (omniwinroot) are stored in an instance of class ActiveTable named tableToAnalyze Next I invoke a member method of ActiveTable (Analyze.Table) on that instance. This executes 2 queries on the table. The first one counts all the records, last one counts only those with distinct index key. If there are less unique index keys then there are rows in the table then the UNunique index keys are safed in a queue (so it could be fixed later). The goal is to copy (by value) tableToAnalyze to a list of instances of ActiveTable when double index keys were found. So something like this:
Class classVar = new Class(); list classList= new List(); classVar.integer1 = 1; classVar.integer2 = 3; if (condition) classList.Add(classVar);
From there i could display in a datagridview and prompt the user to repair (only those tables he wants to repair). The code I have seems to copy by reference. After all the tables are analyzed my list holds 2 (which is correct) of the same values. Both hold the values of the last table What am i doing wrong?private void dubbelNummeringToolStripMenuItem_Click(object sender, EventArgs e) { string sqlQueryTableInfo ="SELECT ctableid, flocation, ctablename, caliasname, cidfield, cidindex, cdatabase FROM tableinfo WHERE (cdatabase = 'dpatient') AND (flocation = 'data') AND (cidfield <> '') ORDER BY ctablename"; string connectionStringTableInfo = "Provider=VFPOLEDB.1;" + "Data Source=" + omniwinRoot + "\\tableinfo.dbf"; OleDbCommand commandTableInfo = new OleDbCommand(sqlQueryTableInfo); ActiveTable tableToAnalyze = new ActiveTable(); List tableToAnalyzeList = new List(); using (OleDbConnection ConnTableInfo = new OleDbConnection(connectionStringTableInfo)) { try { ConnTableInfo.Open(); commandTableInfo.Connection = ConnTableInfo; OleDbDataReader readerTableInfo = commandTableInfo.ExecuteReader(CommandBehavior.CloseConnection); while (readerTableInfo.Read()) { tableToAnalyze.omniwinRoot = omniwinRoot;
-
Hey guys, I'm trying to check the uniqueness of the index key of visual foxpro tables. From the tableinfo table I read all names of the tables in the database and the name of their index key. The name, index key and path (omniwinroot) are stored in an instance of class ActiveTable named tableToAnalyze Next I invoke a member method of ActiveTable (Analyze.Table) on that instance. This executes 2 queries on the table. The first one counts all the records, last one counts only those with distinct index key. If there are less unique index keys then there are rows in the table then the UNunique index keys are safed in a queue (so it could be fixed later). The goal is to copy (by value) tableToAnalyze to a list of instances of ActiveTable when double index keys were found. So something like this:
Class classVar = new Class(); list classList= new List(); classVar.integer1 = 1; classVar.integer2 = 3; if (condition) classList.Add(classVar);
From there i could display in a datagridview and prompt the user to repair (only those tables he wants to repair). The code I have seems to copy by reference. After all the tables are analyzed my list holds 2 (which is correct) of the same values. Both hold the values of the last table What am i doing wrong?private void dubbelNummeringToolStripMenuItem_Click(object sender, EventArgs e) { string sqlQueryTableInfo ="SELECT ctableid, flocation, ctablename, caliasname, cidfield, cidindex, cdatabase FROM tableinfo WHERE (cdatabase = 'dpatient') AND (flocation = 'data') AND (cidfield <> '') ORDER BY ctablename"; string connectionStringTableInfo = "Provider=VFPOLEDB.1;" + "Data Source=" + omniwinRoot + "\\tableinfo.dbf"; OleDbCommand commandTableInfo = new OleDbCommand(sqlQueryTableInfo); ActiveTable tableToAnalyze = new ActiveTable(); List tableToAnalyzeList = new List(); using (OleDbConnection ConnTableInfo = new OleDbConnection(connectionStringTableInfo)) { try { ConnTableInfo.Open(); commandTableInfo.Connection = ConnTableInfo; OleDbDataReader readerTableInfo = commandTableInfo.ExecuteReader(CommandBehavior.CloseConnection); while (readerTableInfo.Read()) { tableToAnalyze.omniwinRoot = omniwinRoot;
Creating a new instance of the ActiveTable variable should do the trick into your while loop while (readerTableInfo.Read()) { tableToAnalyze = new AnalyzeTable(); tableToAnalyze.omniwinRoot = omniwinRoot; tableToAnalyze.table = readerTableInfo.GetString(3).Trim(); tableToAnalyze.tableKey = readerTableInfo.GetString(4).Trim(); tableToAnalyze.AnalyzeTable(); tableToAnalyze.verschil = tableToAnalyze.tableRecords - tableToAnalyze.tableUniqueRecords; if (tableToAnalyze.verschil > 0) { tableToAnalyzeList.Add(tableToAnalyze); } }
-
Creating a new instance of the ActiveTable variable should do the trick into your while loop while (readerTableInfo.Read()) { tableToAnalyze = new AnalyzeTable(); tableToAnalyze.omniwinRoot = omniwinRoot; tableToAnalyze.table = readerTableInfo.GetString(3).Trim(); tableToAnalyze.tableKey = readerTableInfo.GetString(4).Trim(); tableToAnalyze.AnalyzeTable(); tableToAnalyze.verschil = tableToAnalyze.tableRecords - tableToAnalyze.tableUniqueRecords; if (tableToAnalyze.verschil > 0) { tableToAnalyzeList.Add(tableToAnalyze); } }
That did the trick indeed. I thought about it but couldn't understand why this would help. Still don't, hoping to come across an answer in the coming days. (studying c# for work) Anyway, thanks a lot.
-
That did the trick indeed. I thought about it but couldn't understand why this would help. Still don't, hoping to come across an answer in the coming days. (studying c# for work) Anyway, thanks a lot.
Because you were reassigning the properties of the same instance each time. Thats why you got the same object twice in your list because the object is by reference. With the modification, you'll create a new instance of the object on each iteration and each object in your list will be different because they point at their own reference. Hope that help :)
-
Because you were reassigning the properties of the same instance each time. Thats why you got the same object twice in your list because the object is by reference. With the modification, you'll create a new instance of the object on each iteration and each object in your list will be different because they point at their own reference. Hope that help :)
Yes, that helped ;) thanks