How to avoid a series of "if" statements?

Suppose I have a form ... allows WinForm ... with an 'n' number of controls.

I populate them with the default value during LOAD. Then the user gets the opportunity to play with all the controls and set different values ​​for the controls and submit the form. This is where I find that I am writing a series of "if" conditional statements that process the value of each of the controls for (but not limited to), avoiding zeros, performing validation, etc.

Although it works, is there any other more efficient way to do this instead of disparate "ifs"?

+3
source share
8 answers

You may not want to avoid ifs at all, but sometimes it helps to assemble a related group of controls in your form in User Controls. You can then transfer the validation and everything from the Form class to separate user controls, thereby reducing clutter.

+4
source

You should know that WinForms has built-in tools for checking and binding data. Using these built-in capabilities will certainly lead to the creation of code that is better structured and easier to write and maintain than manual coding and verification data. Beth Massey made a series of videos demonstrating these features, which can be found on the MSDN website.

+3
source

** **

, , .

, , , , .
  • , , . (. winforms ( , , , .Net winforms.) )

  • , . , , , , .

  • , .

, , , , . "" , , , , .

+1

1--1 . Validator ControlXValidator, ControlX isValid() , ControlX , getDiagnosticMessage , . Validators, Validator .

validateForm() - :

allvalid = True;
foreach(Validator vtor in allValidators)
{
    if (!vtor.isValid())
    {
        StatusBar.Caption = vtor.getDiagnosticMessage();
        allvalid = False;
        break;
    }
}
+1

( ), , , "" . .

, false, if, : " false, ".

0

, :

( a.k.a) . .

0

..... inputr... ..

0
source

does not apply to any language: use Guard Clauses , as a rule, a good way to get rid of ifs. This is a great way to check for zeros and checks.

0
source

Source: https://habr.com/ru/post/1716959/


All Articles