4

I have a form (form1) which contains a simple drop down list (dropdown1) and 2 textboxes (textbox1) and (textbox2).

The scenario is: user enters some code in textbox1 and then based on what he has entered, "type" of the code will be appeared in textbox2. I should say that textbox2 is Readonly.

Main part of the scenario is that only if textbox2.text is equal to some specific values (type) like "Polymeric, Cord and Seat", the dropdown1 should be appeared in form1 and filled with some colors -get from DB-

Do you think that hard-coding this condition is a right decision? If not, What's your suggestions?

Robert
  • 10,403
  • 14
  • 67
  • 117
odiseh
  • 25,407
  • 33
  • 108
  • 151

5 Answers5

0

Hard-coding is never good if whatever you're hard-coding is subject to change.

For example, you need to add a new type and you have to rebuild everything.

My suggestion: make the mapping between codes and types configurable if you can.

JohnIdol
  • 48,899
  • 61
  • 158
  • 242
  • Do you mean that I should do this in a DataBase? – odiseh Jan 04 '11 at 08:14
  • that's a possibility, store some configuration settings into a db, definitely better than hard-coding. The alternative is a configuration file, but it really depends on how much data. If it's a serious amount then go db. – JohnIdol Jan 04 '11 at 08:15
0

Hardcoding rules like that is never a good thing to.

I would create a class similar to

class Something
{
    public string Keyword;
    public string KeywordType;
    public List<string> ListOfItems;
}

Then I would populate a list of Something's from a database, xml file or anything else, it doesnt matter what.

public List<Something> ListOfSomethings;

read from your data store and add each to the list.

ListOfSomethings.Add(new Something("keyword","KeywordType",new List<String>{"Item1","Item2"});

Then when they type something into textbox1, you can linq or loop the ListOfSomethings to find the item you need, and set textbox2 to Something.KeywordType and then if the count of Something.ListOfItems > 0 then add the items to the combobox and make it visible.

John Petrak
  • 2,898
  • 20
  • 31
0

It's all about the final complexity of your project... if it's a 2 pages project, there is nothing wrong with hard-coded as long it's only once and it's in an accessible place to be changed not only by you but for anyone that take that project in the future.

for example, in your Helper Class add

public const string[] SearchTypes = new string[] { "polymeric", "cord", "seat" };

add an Extension Method to help you

public static string ToJavaScriptArray(this string[] array) {
    string r = "";

    foreach(string s in array)
        r += String.Format("'{0}',", s);

    return r.TrimEnd(',');
}

and then, in your javascript in that particular page you want, you can easily add

var searchTypes = new Array(<%= SearchTypes.ToJavaScriptArray() %>);

and use the javascript array in your code to check if that the "type" is contained in that array, like

let's add a prototype to help us

Array.prototype.has = function(obj) {
    return this.indexOf(obj) >= 0;
}

then

if( searchTypes.has( document.getElementById('textBox1').value ) { 
    // show dropdown        
}

hope it helps.

balexandre
  • 73,608
  • 45
  • 233
  • 342
0

Assuming the you already the logic that determines the type of the code in textbox1, the listbox can be enabled as:

First define an enumeration of the code types,


enum CodeType
{
    Polymeric,
    Cord,
    Seat
}

Then enable / disable the listbox,


public void test()
{
    if (Enum.IsDefined(typeof(CodeType), textBox2.Text.Trim()))
    {
        listBox1.Enabled = true;
    }
    else
    {
        listBox1.Enabled = false;
    }

    //Alternative
    //listBox1.Enabled = Enum.IsDefined(typeof(CodeType), textBox2.Text.Trim());
}

Note that if the listbox is hidden earlier then you need to change the Visible property rather than the Enabled property of the listbox.

If you want to avoid even this, then you can move the type names to a settings file and read then at runtime.

Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
0

As others have said, hard coding is never a good idea. You mentioned that you're accessing a database for the colours, why not have a simple lookup table in the database for the code/type mappings? While that is probably the most complex solution, it is the most robust for a number of reasons.

If you only have a single web server and don't need to worry about data replication, you could just implement the mappings in an XML file and cache the file in memory on a sliding expiration.

DustJones
  • 71
  • 1
  • 2