I’m currently in the process of working in a class design for a web application I’m building. I’m relatively new to OOP (although I have done some). For the most part I’m fairly confident that I know what I’m doing in the paradigm: I know that it’s not good practice for one class to access the internal workings of another class, that unsafe static methods are unsafe because they can modify global state, and that generally speaking, the more purely functional and modular I can keep my code the better off I will be.
I’m a little unsure of what to do in this situation though. I have multiple web pages which all have their own GridView control. Some logic will go through each row and change the color of the row based on certain conditions. Would it be considered bad practice, for example, to keep one static class encapsulating these style changes, which will be accessed by every page? Technically this means that this class would be modifying a member of another class. How should I go about this? I would prefer not to duplicate my code through each class, as I try to adhere to the DRY principle as much as possible.
EDIT: Here’s what I’m thinking.
public static class RowStyle
{
public static void SetRed(GridViewRow row)
{
row.BackColor = Color.Red;
}
// More methods here
}
And each page will pass many GridViewRows to this class, and then have them modified.
Generally, static methods are not so much of a problem if they dont modify static state. Therefore, as long as everything it needs is either encapsulated in the method, or passed in via params then there is less of an issue.
Eg, following your example, the following would IMO be fine
And use that method in each of your pages that need the logic.
However, this would be bad bad bad: