Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard Code List of Years?

Tags:

c#

asp.net

This is the scenario. You've got a web form and you want to prompt the customer to select their birth year.

a) hard code the values in the dropdown list? b) Grab valid years from a DB table

I can see a maintenance nightmare with copying a set of years hard coded in .aspx files everywhere.

updated:

for loop is not ideal (maintenance nightmare and error prone). The user then has to sift through 120 years that haven't even got here yet.

I still like the DB 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

Simple as that. No need to go updating code everywhere. I feel for data that is universal like this, we shouldn't be hard coding this shiza into a bunch of pages which is totally going against reuse and error prone...really it's not pratical. I'd take the hit to the DB for this.

Updated (again...after thinking about this):

Here's my idea. Just create a utility or helper method called GetYears that runs that loop and returns a List<int> back and I can bind that to whatever I want (dropdownlist, etc.). And I like the web.config idea of maintaining that end year.

like image 318
PositiveGuy Avatar asked Nov 27 '22 15:11

PositiveGuy


1 Answers

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

Something as simple as this pseudocode:

for (int i = 1900 ; i < THIS_YEAR - 13 ; i++)
{
    validyears.options.Add(i);
}
like image 113
Mark Rushakoff Avatar answered Dec 05 '22 00:12

Mark Rushakoff