List of hard codes for years?

This is the script. You have a web form, and you want to offer the client to choose the year of birth.

a) hardcode the values โ€‹โ€‹in the drop down list? b) Capture valid years from the DB table

I see a service nightmare that has been copying many years of hard-coded .aspx files everywhere.

update:

for the cycle is not ideal (maintenance nightmare and error prone). Then the user must sift through 120 years that have not yet received here.

I still like the database approach:

* Single point of data * No duplication of code * Update the table as needed to add more years * Year table values could be used for some other dropdown for some other purpose entirely for something other than Birth year 

Just. No need to update the code everywhere. I feel that such data is universal, we should not hard code this schizo into a bunch of pages, which is completely contrary to reuse and error prone ... in fact, this is not practical. I would take a hit in the DB for this.

Updated (again ... thinking about it):

Here is my idea. Just create a utility or helper method GetYears that starts this loop and returns the List<int> back, and I can bind it to what I need (dropdownlist, etc.). And I like the idea of โ€‹โ€‹web.config to support this end of the year.

+4
source share
6 answers

C) Use the for-loop to generate years in the range of your choice.

Something simple like this pseudo code:

 for (int i = 1900 ; i < THIS_YEAR - 13 ; i++) { validyears.options.Add(i); } 
+20
source

None of them provide a centralized service that can decide which mechanism to use, then the application does not care, and you can freely choose hard coding mechanisms, a sliding window or a database.

To expand, as a rule, I would do something like this:

  • Define an IPopulatableYear interface that has one AddYear method that takes an int and creates the corresponding ListItem or something else.
  • Make MyYearListBox inherit from the usual ListBox implementation of IPopulatableYear (this works for winForms or WebForms)
  • Create a static method or singleton or method in your DAL or something else.

Like this:

 PopulateYears(IPopulatableYear pl) { // Very simple implementation - change at will for (int lp = 2009 ; lp < 2009 + 10 ; lp++) { pl.Add(lp); } } 

or

 PopulateYears(IPopulatableYear pl) { // A DB implementation SQLDataReader dr = DAL.YearSet() ; // Your choice of mechanism here while ( dr.Read() ) { pl.Add(dr[YEAR]); } } 

or

 PopulateYears(IPopulatableYear pl) { // A DB limits implementation with different ranges defined in database by key - key determined by control itself - IPopulatableYear needs to implement a .YearSetKey property SQLDataReader dr = DAL.YearLimits(pl.YearSetKey) ; // Your choice of mechanism here for ( int lp = dr[YEAR_MIN] ; lp <= dr[YEAR_MAX] ; lp++ ) { pl.Add(lp); } } 

The mechanism is now centrally managed.

Use MyYearListBox in your forms and call PopulateYears() on it. If your forms are smart, they can detect all instances of MyYearListBox and call it, so you no longer have new code - just drag it.

+2
source

Take a look at Enumerable.Range . I think making DB calls is FAR less efficient than Enumerable.Range.

+2
source

E) Use the text input field because it will always work.

(Be sure to confirm this, of course, as a number. Include โ€œY2Kโ€ and โ€œYear of World War IIโ€ in the dictionary of years, of course.)

+2
source

How you imagine the choice of the year in a web form does not matter. This is an interface solution. Your server should not trust incoming data and should check it accordingly. It is trivial to imitate the presentation form, so it does not matter how it is presented. Heck, you can generate a dropdown using javascript so that there is no download on the server.

You can check with the rule on the server, not on the search.

+1
source

Since you are raising this whole question (and making a bunch of comments), maybe it is in your power to think long and hard about it.

For the end user, itโ€™s hard to beat the ease of use of the text field. Yes, you are going to receive fictitious data, but computers should make everything simpler, not harder. Scrolling through a long list of years to find the year that I know, I was born, this is a nuisance. Especially with all these young shooters and old farts who want to enter their birth years, who are nowhere close to me.

But stepping back even further ... do you really need to ask the user your year of birth? is it important to your application? Could you completely avoid this problem by letting someone else know about it? Tell me, using OpenID, Windows Live ID or Facebook Connect?

+1
source

All Articles