John Bolter
asked on
I have written some spaghetti code
Hi. I am still learning to write C#.
I am writing an ASP.Net application. I have some dynamically created controls and I need to update the values so they are retained on a post back.
I wrote the code below. I was showing it to a friend, and I was proud of my code, and he told me I could do better than that! He also said it was spaghetti code and I knew what he meant.
I have changed the hidden field to a String in my example code, but in ASP.Net it is a HiddenField. I have just converted it to a string and hard coded some of the ID's to make it easy to copy/paste and get working.
Can anyone help me simplify it. I don't think I am wanting a one liner horrible perl type thing that I won't be able to understand tomorrow, but I don't want it to be spaghetti code.
Thank you
I am writing an ASP.Net application. I have some dynamically created controls and I need to update the values so they are retained on a post back.
I wrote the code below. I was showing it to a friend, and I was proud of my code, and he told me I could do better than that! He also said it was spaghetti code and I knew what he meant.
I have changed the hidden field to a String in my example code, but in ASP.Net it is a HiddenField. I have just converted it to a string and hard coded some of the ID's to make it easy to copy/paste and get working.
Can anyone help me simplify it. I don't think I am wanting a one liner horrible perl type thing that I won't be able to understand tomorrow, but I don't want it to be spaghetti code.
Thank you
private String hiddenField = "";
private void updateHiddenFieldForASPNetDynamicallyCreatedControls(String id, Boolean fieldCheckbox)
{
List<String> fields = new List<String>(hiddenField.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries));
Boolean foundField = false;
for (int fieldNumber = 0; fieldNumber < fields.Count; fieldNumber++)
{
//posn 0 = id, posn 1 = boolean true or false, posn 2 ... n = lots of other stuff
String[] values = fields[fieldNumber].Split(new char[] { '\t' });
if (id == values[0])
{
values[1] = fieldCheckbox.ToString();
foundField = true;
StringBuilder replacement = new StringBuilder();
foreach(var value in values)
{
replacement.Append(value);
replacement.Append('\t');
}
fields[fieldNumber] = replacement.ToString().Trim(new char[]{'\t'});
}
}
if (!foundField)
fields.Add(String.Format("{0}\t{1}", id, fieldCheckbox));
hiddenField = "";
foreach (var field in fields)
hiddenField += String.Format("{0}\r", field);
}
private void button1_Click(object sender, EventArgs e)
{
hiddenField = "123\tTrue\t1\t5\tyes\tyes\tundefined\r";
updateHiddenFieldForASPNetDynamicallyCreatedControls("123", true);
updateHiddenFieldForASPNetDynamicallyCreatedControls("123", false);
updateHiddenFieldForASPNetDynamicallyCreatedControls("456", true);
updateHiddenFieldForASPNetDynamicallyCreatedControls("789", true);
updateHiddenFieldForASPNetDynamicallyCreatedControls("456", false);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER