views:

447

answers:

8

Assume, I have a form ...lets say WinForm... with 'n' number of controls.

I populate them with a default value during the LOAD. Then, the user gets to play with all the controls and set different values for the controls and submit the form. Here is where I find myself writing a series of "if" conditional statements handling the value of each of the controls for (but not restricted to) avoiding nulls, doing validation etc.

Though it works, is there some other more efficient way of doing this instead of disparate "ifs" ?

+4  A: 

You may not avoid the 'ifs' entirely, but sometimes it helps to gather related bunch of controls on your Form into User Controls. Then you can move the validation and all from the Form class into individual User Controls, thus reducing clutter.

Frederick
Point taken....what if they are just a bunch of textboxes, datepickers, dropdowns in the same form?
Chouette
IF i remember correctly, since they are just forms, dont we have mechanisms to restrict the data set that can be entered/retrieved from the fields? Like restricting numerals in name field by changing some property of the text field?
Aviator
+3  A: 

You should know that WinForms has build in facilities for both validation and data binding. Using these built-in capabilities will definitely result in code that is better structured and easier to write and maintain than hand coding data and validation operations. Beth Massi has done a series of videos that demonstrates these features, you can find them on the MSDN web site.

Paul Keister
A: 

If you are validating by data-type (dates should look like dates), you could use a function that validates your data and pass the function both the user input and a "sample" of valid data. Valid samples could be stored in an array, keyed by the data-type.

And if the data is not valid, the function returns false and you have one if statement that says "if function returns false, punch the user".

Anthony
+1  A: 

** Edited **

I don't have a catch-all, as this will vary from form to form, but some general advice.

By the way, I love this question because it's all about keeping your code clean, readable, and doing things as simply as possible.

  • Use the included validation controls when possible rather than writing if statements to validate code. (see instruction video for winforms (based on the question I'm assuming you mean .Net winforms.) here)

  • Always look to see if you can write a function to handle repetitive tasks. It takes a line of code to call a function, and if your function is only fivelines long, but you call it tentimes, that means you've saved yourself a lot of duplicate lines of code.

  • If you can write that function to be smart enough and be able to loop through your controls, so much the better.

In short, look at your code and determine to try to do the job with the least amount of code possible while making it easily readable and understandable, and without resorting to bad practices. Experiment in your spare time on non-production "test" code to refine your technique as you learn, but if you get used to thinking about clean code you get better at writing it.

David Stratton
A: 

Assume a decently strong language:

Create a hash (a.k.a Map) with the keys as the control identities and the values as functions. Retrieve the function and call.

John F. Miller
+1  A: 

Create a set of Validators to match 1-for-1 with your controls. Derive from the base Validator a ControlXValidator, which take a ControlX as its constructor, and implements isValid() in the special way that ControlX must evaluate as valid, and implements getDiagnosticMessage to display an appropriate message if the validation fails. Then at the end of your form construction code, create a list of Validators containing the Validator subclass for each control.

Then your validateForm() method can just do something like:

allvalid = True;
foreach(Validator vtor in allValidators)
{
    if (!vtor.isValid())
    {
        StatusBar.Caption = vtor.getDiagnosticMessage();
        allvalid = False;
        break;
    }
}
Paul McGuire
A: 

restrict your control.....life in text box you can set limit of inputr chars ...etc....

A: 

not specific to any language: use Guard Clauses is usually a good way to get rid ifs. It is a excellent way to check nulls and validations.

ez