patterncsharpMinor
Selecting employees and updating a DataGridView
Viewed 0 times
employeesupdatingdatagridviewselectingand
Problem
I've been reading up on different design patterns, mainly MVP and different adaptions of this design pattern / architecture.
Now, I've decided to have a little play with my own ideas, the main outcome separating the user view, from the actual modelling which does all the data changes.
The example is simple, the user is given three radio buttons to select from
When these radio buttons are selected it changes a datagrid view with three columns
My form code has three event driven functions,
To start with, I programatically make some new employees: (Ignore the spelling mistake!)
```
public class employeeData
{
private string classFirstName { get; set; }
private string classLastName { get; set; }
private string classAge { get; set; }
public employeeData(string firstname, string lastname, string age)
{
classFirstName = firstname;
classLastName = lastname;
classAge = age;
}
public string[] updatePerson
Now, I've decided to have a little play with my own ideas, the main outcome separating the user view, from the actual modelling which does all the data changes.
The example is simple, the user is given three radio buttons to select from
- Employee One
- Employee Two
- Employee Three
When these radio buttons are selected it changes a datagrid view with three columns
- Firstname
- Lastname
- Age
My form code has three event driven functions,
radioButton_checkChanged.To start with, I programatically make some new employees: (Ignore the spelling mistake!)
string[] employeeArray = new string[3];
employeeData emplyeeOne = new employeeData("Bob", "Smith", "21");
employeeData emplyeeTwo = new employeeData("John", "Brown", "56");
employeeData emplyeeThree = new employeeData("Andy", "Guy", "28");
private void employeeOne_CheckedChanged(object sender, EventArgs e)
{
updateEmployee(emplyeeOne);
dataGridView1.Rows.Add(employeeArray);
}
private void employeeTwo_CheckedChanged(object sender, EventArgs e)
{
updateEmployee(emplyeeTwo);
dataGridView1.Rows.Add(employeeArray);
}
private void employeeThree_CheckedChanged(object sender, EventArgs e)
{
updateEmployee(emplyeeThree);
dataGridView1.Rows.Add(employeeArray);
}employeeData class (I've tried to go for encapsulation on this):```
public class employeeData
{
private string classFirstName { get; set; }
private string classLastName { get; set; }
private string classAge { get; set; }
public employeeData(string firstname, string lastname, string age)
{
classFirstName = firstname;
classLastName = lastname;
classAge = age;
}
public string[] updatePerson
Solution
- Naming is important,
C#usesPascal casingfor classes and methods, so you should useEmployeeDatainstead ofemployeeData, andUpdatePersoninstead ofupdatePerson
-
Fields live in classes and thus you don't need to give them a
class prefixprivate string FirstName { get; set; }
private string LastName { get; set; }
private string Age { get; set; }-
C# has a
type system that you can use instead of declaring everything as a stringprivate int Age {get;set;}-
updatePerson uses the verb update, and methods with verbs usually do/change things with side-effects but your method just converts the object to a string[], and to be honest, I don't see any need for this method.Code Snippets
private string FirstName { get; set; }
private string LastName { get; set; }
private string Age { get; set; }private int Age {get;set;}Context
StackExchange Code Review Q#69147, answer score: 4
Revisions (0)
No revisions yet.