I used Extract Method on this code because there were multiples of this code, and here is what it became:
private void InsertStatement(string table, string table2, TestURLGUI4.Form1 form, SQLiteConnection sql_con, ref int dbID, ref int dbID2, Chrome chrome, int max)
{
try
{
List<int> dbIDs = new List<int>();
using (SQLiteTransaction mytransaction = sql_con.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(sql_con))
{
mycommand.CommandText = "insert or ignore into " + table + " (id, url, title, visit_count, frecency, last_visit_date) values (@dbID,@url,@title,@visit,@frecency,@time)";
for (var count2 = 0; count2 < chrome.URLs.Count; count2++)
{
URL u = chrome.URLs[count2];
mycommand.Parameters.Add(new SQLiteParameter("@dbID", dbID));
mycommand.Parameters.Add(new SQLiteParameter("@url", u.url));
mycommand.Parameters.Add(new SQLiteParameter("@title", u.title));
mycommand.Parameters.Add(new SQLiteParameter("@visit", u.frequency));
mycommand.Parameters.Add(new SQLiteParameter("@time", ToPRTime(u.visited)));
mycommand.Parameters.Add(new SQLiteParameter("@frecency", ToFrecency(u.frequency)));
mycommand.ExecuteNonQuery();
dbIDs.Add(dbID);
dbID++;
form.label1.Text = count2 + "/" + max;
Application.DoEvents();
}
}
mytransaction.Commit();
}
using (SQLiteTransaction mytransaction = sql_con.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(sql_con))
{
mycommand.CommandText = "insert or ignore into " + table2 + " (id, from_visit, place_id, visit_date, visit_type, session) values (@dbID2,2,@dbID,@time,1, 0)";
for (var count2 = 0; count2 < chrome.URLs.Count; count2++)
{
URL u = chrome.URLs[count2];
mycommand.Parameters.Add(new SQLiteParameter("@dbID2", dbID2));
mycommand.Parameters.Add(new SQLiteParameter("@dbID", dbIDs[count2]));
mycommand.Parameters.Add(new SQLiteParameter("@time", ToPRTime(u.visited)));
mycommand.ExecuteNonQuery();
dbID2++;
form.label1.Text = count2 + "/" + max;
Application.DoEvents();
}
}
mytransaction.Commit();
}
}
catch
{
throw;
}
}
The only problem is, instead of the Chrome type parameter, I have created multiple instances of different classes, and I need to pass each one, for example, I have
IE ie = new IE();
Firefox firefox = new Firefox();
etc. Now, how can I modify my parameter so that instead of Chrome, I can pass Chrome, Firefox, IE, etc. all in the same parameter, one at a time?
All your types are browsers, which means that you can (and should) refactor your code so that they all derive from one shared Browser class, if it’s not already like this. Once you do this, you can polymorphically change your method:
If you are unwilling to do this then the only other solution I can think of is overloading the method.
This article supplies information about inheritance in C#. Basically, what you do is:
You can then still initialize IE like this:
But you can also initialize it polymorphically like this:
If you derive more classes from Browser, such as Firefox, Chrome, etc. then you can treat them all as a single type, Browser, inside a method.