Resolved Referencing Problem

Kostakis45

Active member
Joined
Apr 3, 2022
Messages
37
Programming Experience
Beginner
Hello everyone.After some organizing in my code(It WAS a mess.Everything in one file).I'm having a problem with a specific reference.
To explain it with words first before jumping to code.
In my main Form there is a method with KeyEventArgs so that when the Enter Key is pressed a string is gonna take a value from TextBox.
Ofter organizing my code I made a lot new cs files and managed to reference each to my main Form except one class that is for searching the database.
I'm trying to refernce to from Database_Search.cs to the method with KeyEventArgs in my main form.
Bellow I'll show you some attempts I did with only one not giving me an error but not getting the value from TextBox.
Search Method from Main Form:
public void Search_Box_KeyDown(KeyEventArgs e)//Πατώντας το ENTER
        {
            if(e.KeyCode == Keys.Enter)
            {
                string search_values = Search_Box.Text;
            }
     
            Database_Search ds = new Database_Search();
            ds.Search_The_Database_Enter(e);
        }
1st attempt.This code is in the Database_Search.cs:
public void Search_The_Database_Enter(KeyEventArgs e)
        {
            Plasteka pa = new Plasteka();//My main Form
          

            if (e.KeyCode == Keys.Enter)
            {
                string search_values = pa.Search_Box.Text;//Dunno if this line should be inside if statement            // Τιμή από το Search_Box
                pa.tabControl1.Visible = true;

                var con = new SQLiteConnection(connection);
                con.Open();
                var cmd = new SQLiteCommand(con);
                cmd.CommandText = "SELECT * FROM Description WHERE Mould_Code = '" + search_values + "'";
                SQLiteDataReader reader = cmd.ExecuteReader();
                reader.Read();

                //Περιγραφή
                pa.mould_code_input.Text = reader["Mould_Code"].ToString();
                pa.machine_number_input.Text = reader["Machine_Number"].ToString();
                pa.machine_type_input.Text = reader["Machine_Type"].ToString();
                pa.supplier_input.Text = reader["Supplier"].ToString();
                pa.colour_input.Text = reader["Colour"].ToString();
                pa.comboBox1.Text = reader["Plastic_Type_1"].ToString();
                pa.comboBox2.Text = reader["Plastic_Type_2"].ToString();
                pa.comboBox3.Text = reader["Plastic_Type_3"].ToString();
                pa.numericUpDown1.Text = reader["Plastic_Type_1_Val"].ToString();
                pa.numericUpDown2.Text = reader["Plastic_Type_2_Val"].ToString();
                pa.numericUpDown3.Text = reader["Plastic_Type_3_Val"].ToString();
                pa.dateTimePicker1.Text = reader["Date_Time"].ToString();
                pa.item_name_input.Text = reader["Item_Name"].ToString();

                byte[] Image_1 = (byte[])reader["Image_1"];
                pa.pictureBox1.Image = ConvertBytesToImage(Image_1);
                byte[] Image_2 = (byte[])reader["Image_2"];
                pa.pictureBox2.Image = ConvertBytesToImage(Image_2);         
            }
        }
2nd attempt.Gives conversion Error Void to String.Fully Understandable.:
string search_values = pa.Search_Box_KeyDown(search_values)
3d attempt.Gives error that i can't use (.) in void.I get that too.:
cmd.CommandText = "SELECT * FROM Description WHERE Mould_Code = '" + pa.Search_Box_KeyDown(e).search_values; + "'";

The 1st attempt doesn't give errors but it's not getting the value.
So my question is how can I reference to that method so that i can pass the search_valu to my database?
 
Last edited:
I'm trying to refernce to from Database_Search.cs to the method with KeyEventArgs in my main form.
This suggests that you have done things wrong when organising your code. The form should be using the other classes and calling their methods, not the other way around. For one thing, a KeyEventArgs is not something that you should generally be passing out of your form because it is specific to the UI. Why does that Search_The_Database_Enter need the KeyEventArgs value? All it's doing is checking for the Enter key and then acting only if it finds it. You're already checking for the Enter key back in the form though, so why not just call that Search_The_Database_Enter if and only if the Enter key was pressed and then pass it the value you want to search for?

That method should not even know the form exists. It should receive the value to search for, do the search and return the data it retrieves. It's then up to whoever called the method to decide what to do with that data. You could call it from that form and the form can populate its own controls, but you could also call it from a web app or a Console app. That is called "loose coupling", where specific dependencies only work in one direction. If both classes rely on each other then they will only work together. If one class is independent then you can use it in any situation where its functionality would be useful. If you're going to separate your data access code then that layer should contain ONLY data access code, but you have still coupled it to the UI code.
 
This suggests that you have done things wrong when organising your code. The form should be using the other classes and calling their methods, not the other way around. For one thing, a KeyEventArgs is not something that you should generally be passing out of your form because it is specific to the UI. Why does that Search_The_Database_Enter need the KeyEventArgs value? All it's doing is checking for the Enter key and then acting only if it finds it. You're already checking for the Enter key back in the form though, so why not just call that Search_The_Database_Enter if and only if the Enter key was pressed and then pass it the value you want to search for?

That method should not even know the form exists. It should receive the value to search for, do the search and return the data it retrieves. It's then up to whoever called the method to decide what to do with that data. You could call it from that form and the form can populate its own controls, but you could also call it from a web app or a Console app. That is called "loose coupling", where specific dependencies only work in one direction. If both classes rely on each other then they will only work together. If one class is independent then you can use it in any situation where its functionality would be useful. If you're going to separate your data access code then that layer should contain ONLY data access code, but you have still coupled it to the UI code.
Damn.That was a stupid mistake for wich i was aware off.Just slipped my mind completely.Thanks for pinpointing.I should take a break sometimes.I'm going to fix everything and report back.
 

Latest posts

Back
Top Bottom