.net: hard-coded conditions or WHAT?

I have a form (form1) that contains a simple drop down list (dropdown1) and 2 text fields (textbox1) and (textbox2).

Scenario: the user enters some code into text box 1, and then, based on what he has entered, the "type" of code will appear in text box 2. I have to say that textbox2 is Readonly.

The main part of the script is that only if textbox2.text is equal to some specific values ​​(types), such as "Polymer, cord and seat", dropdown1 should be displayed in form1 and filled with some colors -get from DB-

Do you think hard coding of this condition is the right solution? If not, what are your suggestions?

+4
source share
5 answers

Hard coding is never good if what you hard code is subject to change.

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

My suggestion: do a mapping between codes and types if you can.

0
source

Hard coding rules like these are never good.

I would create a class similar to

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

Then I would populate the Something list from the database, xml file or everything else, it doesn't matter.

 public List<Something> ListOfSomethings; 

read from your data warehouse and add them to the list.

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

Then, when they type something in textbox1, you can linq or encode ListOfSomethings to find the item you want, and set textbox2 to Something.KeywordType , and then , if counting something. ListOfItems> 0 , then add items to the combo box and make them visible.

0
source

All about the ultimate complexity of your project ... if it is a 2-page project, there is nothing wrong with hard-coded so far only once and it is in an accessible place that will be changed not only by you, but also by those who will take this project in the future.

for example, in the 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 on 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 the type is in it, for example

add 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 this helps.

0
source

Assuming that you are already the logic that determines the type of code in text box 1, the list can be included as:

First define the numbering of the code types,

 enum CodeType { Polymeric, Cord, Seat } 

Then enable / disable the list,

 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 list is previously hidden, you need to change the Visible property, not the Enabled property in the list.

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

0
source

As others have said, hard coding is never a good idea. You mentioned that you are accessing the database for colors, why not have a simple database lookup table for code type / type mappings? Although this is probably the most difficult solution, it is the most reliable for a number of reasons.

If you have only one web server and you do not need to worry about data replication, you can simply implement the mappings in the XML file and cache the file in memory with an expiration date.

0
source

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


All Articles