user2469932
user2469932

Reputation: 167

How do I call a method from a Form class?

Right now I have this code in a class called HelperClass:

class HelperClass : CollegeSystem.GUI
    {

        public void selectStudent()
        {
             MySqlConnection conn; // connection object;
        string connstring = "server=localhost;user Id=root;database=collegesystem;Convert Zero Datetime=True ";

            try
            {
                conn = new MySqlConnection(connstring);
                conn.Open();
                MySqlCommand myCommand = new MySqlCommand("SELECT * FROM person", conn);
                MySqlDataReader myReader;
                myReader = myCommand.ExecuteReader();
                cmbTable.Items.Clear();
                while (myReader.Read())
                {
                    cmbTable.Items.Add(myReader["personID"] + " | " + myReader["firstName"] + " | " + myReader["lastName"] + " | " + myReader["address"] + " | " + myReader["phoneNumber"] + " | " + myReader["postCode"] + " | " + myReader["dateOfBirth"]);
                }
            }

            catch (Exception err)
            {//handle the error with a message
                lblInfo.Text = " Error reading the database.";
                lblInfo.Text += err.Message; ;
            }

            finally
            {

            }

And I would like to call that from my form, but it doesn't work:

 private void fillcomboBox()
 {
     selectStudent();
 }

The name "selectStudent" does not exist in the current context

I have placed this at the top of the form:

 public partial class GUI : Form, College_System.HelperClass

am I missing something?

PS. Why would the cmbTable and lblInfo be inaccessible due to it's protection level? Thank you.

Upvotes: 1

Views: 132

Answers (3)

Krishna Sarma
Krishna Sarma

Reputation: 1860

You have multiple issues with this code.

  1. You have a class and an instance method, and you are calling the method directly. You should create instance of the class and call the method.

  2. Check if the class definition and call are in same namespace or not. If not, you should define the class as internal/public and include the namespace in the calling code page.

  3. You can't access the label unless they are defined in the same class (the helper)

In general, helper classes are standalone classes, where you pass the data to its methods and get result for processing. For instance, you can try something like:

class myForm : Form
{
    protected void OnClick()
    {
        try
        {
            var data = Helper.SelectStudent();
            cmbTable.Items.AddRange(data);
        }
        catch (Exception ex)
        {
            lblInfo.Text = " Error reading the database.";
            lblInfo.Text += err.Message; ;
        }
    }
}

class HelperClass
{
    public static List<string> SelectStudent()
    {
        List<string> data = new List<string>();
        MySqlConnection conn; // connection object;
        string connstring = "server=localhost;user Id=root;database=collegesystem;Convert Zero Datetime=True ";
        conn = new MySqlConnection(connstring);
        conn.Open();
        MySqlCommand myCommand = new MySqlCommand("SELECT * FROM person", conn);
        MySqlDataReader myReader;
        myReader = myCommand.ExecuteReader();
        cmbTable.Items.Clear();
        while (myReader.Read())
        {
            data.Add(myReader["personID"] + " | " + myReader["firstName"] + " | " + myReader["lastName"] + " | " + myReader["address"] + " | " + myReader["phoneNumber"] + " | " + myReader["postCode"] + " | " + myReader["dateOfBirth"]);
        }
        return data;
    }
}

Upvotes: 1

Ehsan
Ehsan

Reputation: 32681

public partial class GUI : Form, College_System.HelperClass

you can't do this. Your line

public partial class GUI : Form, College_System.HelperClass

should be

public partial class GUI : Form

then you can call like

College_System.HelperClass c = new College_System.HelperClass();
c.selectStudent();

Upvotes: 1

tdelepine
tdelepine

Reputation: 2016

By default the attribute are private. If you want use in herited classe you can choice : public protected,conteneur,protectedinternal

In your case i suggest public

 public class HelperClass : CollegeSystem.GUI

Upvotes: 1

Related Questions