Class question

Jelmer

Regular
Joined
Jan 11, 2006
Messages
96
Í 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:

Code:
        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:
Code:
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
 
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:

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);
            }
        }
 
thats right !
It works .. thnx !

And indeed return hoeveelheid...

But it doesn't work so i tried a hard integer..
 
Back
Top