944,103 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1952
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 10th, 2009
0
Re: GTK Hangs & An Index Error
New code:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using Gtk;
  3. using Mono.Data.Sqlite;
  4. using System.IO;
  5.  
  6. public partial class MainWindow: Gtk.Window
  7. {
  8. //SQLite variables
  9. static SqliteConnection DataConn;
  10. static SqliteCommand DataComm = new SqliteCommand();
  11. static SqliteDataReader DataReader;
  12. string dsn;
  13.  
  14. //tree_1 variables
  15. Gtk.TreeViewColumn name_1 = new Gtk.TreeViewColumn ();
  16. Gtk.ListStore List_1 = new Gtk.ListStore (typeof (string));
  17. Gtk.CellRendererText NameCell_1 = new Gtk.CellRendererText ();
  18.  
  19. //tree_2 variables
  20. Gtk.TreeViewColumn home_team_2 = new Gtk.TreeViewColumn ();
  21. Gtk.TreeViewColumn away_team_2 = new Gtk.TreeViewColumn ();
  22. Gtk.TreeStore List_2 = new Gtk.TreeStore (typeof (string), typeof (string));
  23. Gtk.CellRendererText homeCell_2 = new Gtk.CellRendererText ();
  24. Gtk.CellRendererText awayCell_2 = new Gtk.CellRendererText ();
  25.  
  26. //tree_3 variables
  27. Gtk.TreeViewColumn home_team_3 = new Gtk.TreeViewColumn ();
  28. Gtk.TreeViewColumn away_team_3 = new Gtk.TreeViewColumn ();
  29. Gtk.TreeViewColumn home_team_score_3 = new Gtk.TreeViewColumn ();
  30. Gtk.TreeViewColumn away_team_score_3 = new Gtk.TreeViewColumn ();
  31. Gtk.TreeStore List_3 = new Gtk.TreeStore (typeof (string), typeof (string), typeof (string), typeof (string));
  32. Gtk.CellRendererText homeCell_3 = new Gtk.CellRendererText ();
  33. Gtk.CellRendererText awayCell_3 = new Gtk.CellRendererText ();
  34. Gtk.CellRendererText homescoreCell_3 = new Gtk.CellRendererText ();
  35. Gtk.CellRendererText awayscoreCell_3 = new Gtk.CellRendererText ();
  36.  
  37. //Iter
  38. Gtk.TreeIter iter_2;
  39. Gtk.TreeIter iter_3;
  40.  
  41. int numofteams;
  42. int numofteamsOLD;
  43. string[] teamsa;
  44. string[] homea;
  45. string[] awaya;
  46. int[,] fixturea;
  47.  
  48. public void generate_fixtures()
  49. {
  50. Random rand = new Random();
  51. int home;
  52. int away;
  53. int numoffix = numofteams/2;
  54. int j = 0;
  55. homea = new string[numoffix];
  56. awaya = new string[numoffix];
  57. int[] teamsused = new int[numofteams];
  58. int k = 0;
  59. for (int i = 0; i < numoffix; i++){
  60. do{
  61. home = rand.Next(0, numofteams);
  62. away = rand.Next(0, numofteams);
  63. k++;
  64. } while (home == away || fixturea[home,away] != 0 || teamsused[home] != 0 || teamsused[away] != 0);
  65. fixturea[home,away] = 1;
  66. System.Diagnostics.Debug.Assert(i == j && j < numoffix && numoffix == homea.Length);
  67. homea[j] = "";
  68. homea[j] = teamsa[home];
  69. homea[j] = teamsa[home];
  70. awaya[j] = teamsa[away];
  71. teamsused[home] = 1;
  72. teamsused[away] = 1;
  73. j++;
  74. }
  75. }
  76.  
  77. public void generate_all_fixtures()
  78. {
  79. fixturea = new int[numofteams,numofteams];
  80. DataComm.CommandText = "DELETE FROM fixture;";
  81. DataComm.ExecuteNonQuery();
  82. int blah = (numofteams-1) * 2;
  83. for (int i = 0; i < blah; i++){
  84. generate_fixtures();
  85. for (int j = 0; j < numofteams/2; j++){
  86. DataComm.CommandText = string.Format("INSERT INTO fixture VALUES (null, \"{0}\", \"{1}\", {2});", homea[j], awaya[j], i+1);
  87. DataComm.ExecuteNonQuery();
  88. DataComm.CommandText = string.Format("INSERT INTO results VALUES (null, \"{0}\", null, \"{1}\", null, {2});", homea[j], awaya[j], i+1);
  89. DataComm.ExecuteNonQuery();
  90. }
  91. }
  92. tree_2_add_data();
  93. }
  94.  
  95. public void generate_team_array()
  96. {
  97. DataComm.CommandText = "SELECT * FROM teams;";
  98. DataReader = DataComm.ExecuteReader();
  99. teamsa = new string[numofteams];
  100. int i = 0;
  101. string returned;
  102. while (DataReader.Read()){
  103. returned = DataReader["Name"].ToString();
  104. teamsa[i] = returned;
  105. i++;
  106. }
  107. DataReader.Close();
  108. }
  109.  
  110. public void tree_1_add_data()
  111. {
  112. numofteams = 0;
  113. DataComm.CommandText = "SELECT * FROM teams;";
  114. DataReader = DataComm.ExecuteReader();
  115. string returned;
  116. while (DataReader.Read()){
  117. returned = DataReader["Name"].ToString();
  118. List_1.AppendValues(returned);
  119. numofteams += 1;
  120. }
  121. DataReader.Close();
  122. generate_team_array();
  123. }
  124.  
  125. public void tree_2_add_data()
  126. {
  127. string day;
  128. List_2.Clear();
  129. for (int i = 0; i < numofteams/2; i++){
  130. DataComm.CommandText = string.Format("SELECT * FROM fixture where Day = {0};", i+1);
  131. DataReader = DataComm.ExecuteReader();
  132. if (DataReader.HasRows){
  133. day = string.Format("Day {0}", i+1);
  134. iter_2 = List_2.AppendValues(day);
  135. } else {
  136. DataReader.Close();
  137. return;
  138. }
  139. while (DataReader.Read()){
  140. List_2.AppendValues(iter_2, DataReader["Home"].ToString(), DataReader["Away"].ToString());
  141. numofteams += 1;
  142. }
  143. DataReader.Close();
  144. }
  145. }
  146.  
  147. public void tree_3_add_data(){
  148. string day;
  149. List_3.Clear();
  150. for (int i = 0; i < numofteams/2; i++){
  151. DataComm.CommandText = string.Format("SELECT * FROM results where Day = {0};", i+1);
  152. DataReader = DataComm.ExecuteReader();
  153. if (DataReader.HasRows){
  154. day = string.Format("Day {0}", i+1);
  155. iter_3 = List_3.AppendValues(day);
  156. } else {
  157. DataReader.Close();
  158. return;
  159. }
  160. while (DataReader.Read()){
  161. List_3.AppendValues(iter_3, DataReader["Home"].ToString(), DataReader["Home_Score"].ToString(), DataReader["Away"].ToString(), DataReader["Away_Score"].ToString());
  162. numofteams += 1;
  163. }
  164. DataReader.Close();
  165. }
  166. }
  167.  
  168. public void tree_conf()
  169. {
  170. //tree_1
  171. name_1.Title = "Name";
  172. tree_1.AppendColumn(name_1);
  173. tree_1.Model = List_1;
  174. name_1.PackStart (NameCell_1, true);
  175. name_1.AddAttribute (NameCell_1, "text", 0);
  176. tree_1_add_data();
  177.  
  178. //tree_2
  179. home_team_2.Title = "Home Team";
  180. away_team_2.Title = "Away Team";
  181. tree_2.AppendColumn(home_team_2);
  182. tree_2.AppendColumn(away_team_2);
  183. tree_2.Model = List_2;
  184. home_team_2.PackStart(homeCell_2, true);
  185. home_team_2.AddAttribute(homeCell_2, "text", 0);
  186. away_team_2.PackStart(awayCell_2, true);
  187. away_team_2.AddAttribute(awayCell_2, "text", 1);
  188. tree_2_add_data();
  189.  
  190. //tree_3
  191. home_team_3.Title = "Home Team";
  192. away_team_3.Title = "Away Team";
  193. home_team_score_3.Title = "Home Score";
  194. away_team_score_3.Title = "Away Score";
  195. tree_3.AppendColumn(home_team_3);
  196. tree_3.AppendColumn(home_team_score_3);
  197. tree_3.AppendColumn(away_team_3);
  198. tree_3.AppendColumn(away_team_score_3);
  199. tree_3.Model = List_3;
  200. home_team_3.PackStart(homeCell_3, true);
  201. home_team_3.AddAttribute(homeCell_3, "text", 0);
  202. home_team_score_3.PackStart(homescoreCell_3, true);
  203. home_team_score_3.AddAttribute(homescoreCell_3, "text", 1);
  204. away_team_3.PackStart(awayCell_3, true);
  205. away_team_3.AddAttribute(awayCell_3, "text", 2);
  206. away_team_score_3.PackStart(awayscoreCell_3, true);
  207. away_team_score_3.AddAttribute(awayscoreCell_3, "text", 3);
  208. tree_3_add_data();
  209. }
  210.  
  211. public MainWindow (): base (Gtk.WindowType.Toplevel)
  212. {
  213. Build ();
  214. bool first = false;
  215. if (!File.Exists("league.db")){
  216. SqliteConnection.CreateFile("league.db");
  217. first = true;
  218. }
  219. string dsn = "Data Source=league.db;";
  220. DataConn = new SqliteConnection(dsn);
  221. DataConn.Open();
  222. DataComm.Connection = DataConn;
  223. if (first){
  224. DataComm.CommandText = "CREATE TABLE teams (ID INTEGER PRIMARY KEY, Name TEXT);";
  225. DataComm.ExecuteNonQuery();
  226. DataComm.CommandText = "CREATE TABLE fixture (ID INTEGER PRIMARY KEY, Home TEXT, Away TEXT, Day INTEGER);";
  227. DataComm.ExecuteNonQuery();
  228. DataComm.CommandText = "CREATE TABLE results (ID INTEGER PRIMARY KEY, Home TEXT, Home_Score INTEGER, Away TEXT, Away_Score INTEGER, Day INTEGER);";
  229. DataComm.ExecuteNonQuery();
  230. }
  231. tree_conf();
  232. }
  233.  
  234. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  235. {
  236. Application.Quit ();
  237. a.RetVal = true;
  238. }
  239.  
  240. protected virtual void on_submit_1 (object sender, System.EventArgs e)
  241. {
  242. //if it's empty don't do anything
  243. string team = team_1.Text;
  244. if (team == ""){
  245. return;
  246. }
  247. //Insert data into the table
  248. DataComm.CommandText = string.Format("INSERT INTO teams VALUES (null, \"{0}\");", team);
  249. DataComm.ExecuteNonQuery();
  250. //Clear tree_1
  251. List_1.Clear();
  252. //Query database for teams
  253. tree_1_add_data();
  254. }
  255.  
  256. protected virtual void on_generate_2 (object sender, System.EventArgs e)
  257. {
  258. generate_all_fixtures();
  259. }
  260.  
  261. protected virtual void on_submit_3 (object sender, System.EventArgs e)
  262. {
  263. }
  264. }
I don't know how to install SQLite in Windows, I'm running Ubuntu.
Thanks for trying to help
Last edited by pymatio; Oct 10th, 2009 at 2:07 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Oct 10th, 2009
0
Re: GTK Hangs & An Index Error
If you've installed SQLite & created a new project right-click on the references & check Mono.SQLite (or whatever it is)
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Oct 10th, 2009
0
Re: GTK Hangs & An Index Error
OK, that error is indicating that home index is out of range because that is line 68 and it did not fail at line 67, which is where we have already ensured that "j" is not out of range because that line did not throw the error:

C# Syntax (Toggle Plain Text)
  1. homea[j] = ""; // line 67 OK
  2. homea[j] = teamsa[home]; // line 68 failed

EDIT: What is the value of "numofteams" when this is executing?
Last edited by DdoubleD; Oct 10th, 2009 at 2:39 pm.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 11th, 2009
0
Re: GTK Hangs & An Index Error
Click to Expand / Collapse  Quote originally posted by DdoubleD ...
OK, that error is indicating that home index is out of range because that is line 68 and it did not fail at line 67, which is where we have already ensured that "j" is not out of range because that line did not throw the error:

C# Syntax (Toggle Plain Text)
  1. homea[j] = ""; // line 67 OK
  2. homea[j] = teamsa[home]; // line 68 failed

EDIT: What is the value of "numofteams" when this is executing?
16, which is 4x what it should be.
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Oct 12th, 2009
0
Re: GTK Hangs & An Index Error
So, did you determine the discrepancy in "numofteams" and get the index out of range fixed?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 12th, 2009
0
Re: GTK Hangs & An Index Error
Not yet
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Oct 14th, 2009
0
Re: GTK Hangs & An Index Error
Bump
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Oct 15th, 2009
0
Re: GTK Hangs & An Index Error
Take a look at all the DataReader loops where you are incrementing numofteams for all of the various list loads...

Without the ability to up and run this project, it's difficult to follow everything that might be happening. What I advise, for starters, is to not rely on numofteams increment as it is currently, and just use the teamsa.Length property for determining the actual number of teams you have loaded into the teamsa string array:

C# Syntax (Toggle Plain Text)
  1. // set the var to be whatever the actual number of teams loaded is...
  2. int numOfTeams = teamsa.Length;
  3. // then, assuming number of fixed teams is half the even portion of total teams...
  4. int numOfFixed = numOfTeams / 2;
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Global property
Next Thread in C# Forum Timeline: Dictionary Program. help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC