What is encapsulation:
Encapsulation can divide a program into multiple "blocks" according to certain rules, and the block may be associated. Each block has a variable part and a stable part. We need to separate the variable part and the stable part, expose the stable part to other parts, and hide the variable part so that it can be modified at any time. This job is encapsulation.
Encapsulation inheritance polymorphism is three major features of object-oriented language. Encapsulation is the most basic and important point. Without encapsulation, classes, objects, attributes, and methods,
In turn, it is because of encapsulation that you can turn everything in reality into a custom class in your mind. In this way, a living object is derived.
You also let all objects have their own attributes and methods to express your thoughts.
Encapsulation:
Encapsulation reduces the amount of code and prevents code redundancy. Instead of writing duplicate code, we can encapsulate the same code into a method. when we need it, you only need to call this method. Encapsulation ensures data integrity without affecting the interaction between callers and classes when technical details need to be modified.
The purpose of encapsulation is to make the type safe and easy to use. The required members are exposed to the user and do not need to be protected.
For example:
Add four ComboBox controls to winfrom. Display the required information to the four ComboBox controls.
We can take the code that needs to be repeatedly written to the following datatable method with parameters.
Then call this method through parameters.
String STR = "query statement ";
Datatable dt = getdate (STR, null); (STR is our query statement ).
This. combobox1.datasource = DT;
This. combobox1.displaymember = "d_name"; (displaymember displays the data we want in the combobox1 control .)
This. combobox1.valuemember = "d_id"; (valuemenber will also find the ID corresponding to d_name, but it will not be displayed. When we need ID, selectedvalue will be enough)
Private datatable getdate (string sqlstr, sqlparameter parameter)
{
Sqlconnection conn = new sqlconnection (STR); (this STR is used to connect to the database)
Sqlcommand comm = new sqlcommand (sqlstr, Conn );
If (parameter! = NULL)
{
Comm. Parameters. addwithvalue (parameter. parametername, parameter. value );
}
Sqldataadapter adapter = new sqldataadapter (Comm );
Datatable dt = new datatable ();
Adapter. Fill (DT );
Return DT;
}
We can also use this method to display the information of the remaining ComboBox controls.
Because I am a beginner, I am a newbie. I hope I can bear it with me...
C # Encapsulation Method