-4

Good Morning,

I am working on WindowsForm. I came up with 2 solutions. I wanted to know which solution is good practice to follow?

Solution 1: I have a written a common static methods for validation like phone-text box, mandatory_textbox-key press etc.I have many common methods like this. So what i did is i created a utility class and placed all these static methods in it. Then used these methods across the application.

Solution 2: i got an idea, so what i did is i created a baseform-inherited Form class, Then i inherited this baseform in all the other forms(Multi-level inheritance).

In the baseform i moved all the validation methods from Utility class and made then non-static.

I also taught about UserControl. If i do that i have work with the alignment tasks again. So that only came up with the two solutions

So Can you suggest which to follow?

user2115618
  • 183
  • 1
  • 1
  • 13
  • http://stackoverflow.com/questions/798036/how-to-decide-between-c-sharp-static-and-non-static-methods?rq=1 – Natrium Oct 07 '16 at 05:20
  • http://stackoverflow.com/questions/11027540/should-i-go-with-static-methods-or-non-static-methods?rq=1 – Natrium Oct 07 '16 at 05:21
  • 1
    You can also create a custom TextBox by deriving from Textbox. – CSharpie Oct 07 '16 at 05:36
  • @CSharpie I taught about custom control . If i do that i have work with the alignment tasks again. So that only came up with the two solutions – user2115618 Oct 13 '16 at 10:36

4 Answers4

1

You can move the static methods inside non static classes, and pass concrete objects (maybe through interfaces) to the classes/methods who needs that functionality. This way you keep your code easy to test, and decoupled.

By example if you have a class PhoneNumberValidator implementing the interface IValidator which have a method bool Validate(string phoneNumber), and pass it where you need to validate a phone number.

I guess this wuould be the best practice to have a decoupled application.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 1
    Afraid Windows forms developers didn't like this solution, because you need create more classes, interfaces, change constructors to pass interface implementation there - too much only for writing tests which increase amount of files :) – Fabio Oct 07 '16 at 05:55
  • @Fabio the question is about the best practice, so I am sorry for my answer :) – meJustAndrew Oct 07 '16 at 09:09
0

Use User Control instead of a separate form, if these common controls are being used in every form. Static methods are supposed to be used for utils kind of requirements.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • I taught about User control . If i do that i have work with the alignment tasks again. So that only came up with the two solutions – user2115618 Oct 13 '16 at 10:37
0

Some times ago I tried to use both solution described by you for different tasks. Every of this have own pluses and minuses.

  1. So, in first case we have only one ststic class and one implementation of every static methods in memory. We can apply this methods to any quantity of other object instances. But we need access this class in every namespace where we will use it. So, if we will make some changes in any code of this class, it will be applied to all object instances related this class. Sometimes it's convenient, sometimes not.

  2. In second case we will got new instance of base form in memory (less efficient), but we also have one base implementation of methods for inherited forms like first approach. As additional benefit we always can override methods for special cases (if it's needed) for some specific instances only.

In any case, only you can make right decision based on your task context.

0

There in no straightforward answer on whether one should declare methods as static or not. It depends on the context and functionality of your application.

Going with some assumptions for your situation, consider following thoughts on high level -

  • If the validation is related to one particular form only, and not applicable for other forms, declare it within your form class ans private method. If these validations do not require any class instance, you may declare them static.
  • If some validations are common for different form, you may declare them as static. Do take caution and do not pass controls to these methods, instead pass the values that you want to validate for better design.
  • Consider declaring validations in base form only if those are applicable to all or if not most of the forms, and again, if they do not use any instance object, you may mark them as static

A good discussion here.

Yogi
  • 9,174
  • 2
  • 46
  • 61