Jelmer Posted February 2, 2006 Posted February 2, 2006 Í build a class file. But a little problem. I like it that the class has a function that returns something (an integer in this case) to the main file. So this is the incorrect part of my class: public void aantal_klanten() { int hoeveelheid; try { OleDbConnection Connection = new OleDbConnection(); Connection = dbase(Connection); // Create an OleDb command, OleDbCommand myCommand = new OleDbCommand("count (*) from Bedrijven", Connection); Connection.Open(); OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); while (myReader.Read()) { hoeveelheid = Convert.ToInt32(myReader.GetString(0)); } Connection.Close(); return hoeveelheid; } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } It shoud not be a void i guess.. but i forgot what it must be.. Inside the class i use this for example, that works: static OleDbConnection dbase(OleDbConnection Connection) The class is named: Class_customers: Class_customers klant = new Class_customers(); So i like to use this kind of code: int counting; counting = klant.aantal_klanten(); Whats the correct syntax ? Gr. Jelmer Quote
Jelmer Posted February 2, 2006 Author Posted February 2, 2006 I'll changed it to this, i think this is good.. but i'll get the following error; .aantalklanten()': not all code paths return a value Code: public int aantalklanten() { int hoeveelheid; try { OleDbConnection Connection = new OleDbConnection(); Connection = dbase(Connection); // Create an OleDb command, OleDbCommand myCommand = new OleDbCommand("count (*) from Bedrijven", Connection); Connection.Open(); OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); while (myReader.Read()) { hoeveelheid = Convert.ToInt32(myReader.GetString(0)); } Connection.Close(); return 10; } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } Quote
Administrators PlausiblyDamp Posted February 2, 2006 Administrators Posted February 2, 2006 You also need to make sure the catch block also returns a value. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted February 2, 2006 *Experts* Posted February 2, 2006 And I assume you want to "return hoeveelheid;" instead of "return 10;" -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Jelmer Posted February 3, 2006 Author Posted February 3, 2006 thats right ! It works .. thnx ! And indeed return hoeveelheid... But it doesn't work so i tried a hard integer.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.