I have found myself referencing controls on a form (think Windows forms but more proprietary) by hard-typing their names in over and over again. Often I'll have a number of similar controls which could be named coolControl1department
, coolControl2department
, coolControl1jobTitle
, coolControl2jobTitle
, etc. This becomes tedious and a nightmare to maintain. Renaming the controls is definitely an option but I would still need some way of generating their names to assign values and whatnot.
To make this less reliant on me typing in the names I've created a dictionary method to concatenate the various parts of the control names, including unique reference numbers, and then call the final values elsewhere.
While it's more centralised than it was previously, there is still room for improvement and I'd appreciate any suggestions to streamline this.
Note that I'm only able to use C# version 4.0 in this application. Code below.
public void DoStuffWithControls()
{
Dictionary<string, string> myControls1 = GeneratedControlNames(1);
Dictionary<string, string> myControls2 = GeneratedControlNames(2);
string genericControl1;
string genericControl2;
string departmentControl1;
string departmentControl2;
myControls1.TryGetValue("generic", out genericControl1);
myControls1.TryGetValue("department", out departmentControl1);
myControls2.TryGetValue("generic", out genericControl2);
myControls2.TryGetValue("department", out departmentControl2);
EnquiryForm.GetEnquiryControl(genericControl1, EnquiryControlMissing.Exception).Value = "some value";
EnquiryForm.GetEnquiryControl(departmentControl1, EnquiryControlMissing.Exception).Value = "some value";
EnquiryForm.GetEnquiryControl(genericControl1, EnquiryControlMissing.Exception).AnotherProperty = "some other value";
EnquiryForm.GetEnquiryControl(genericControl2, EnquiryControlMissing.Exception).Value = "some value";
EnquiryForm.GetEnquiryControl(genericControl2, EnquiryControlMissing.Exception).AnotherProperty = "some other value";
EnquiryForm.GetEnquiryControl(departmentControl2, EnquiryControlMissing.Exception).Value = "some value";
}
private static Dictionary<string, string> GeneratedControlNames(int refNumber)
{
var genericPrefix = "coolControl";
var departmentControlSuffix = "Department";
var jobTitleControlSuffix = "Title";
var directDialControlSuffix = "DDI";
var emailDialControlSuffix = "Email";
var dict = new Dictionary<string, string>();
dict.Add("generic", string.Concat(genericPrefix, refNumber));
dict.Add("department", string.Concat(genericPrefix, refNumber, departmentControlSuffix));
dict.Add("jobTitle", string.Concat(genericPrefix, refNumber, jobTitleControlSuffix));
dict.Add("directDial", string.Concat(genericPrefix, refNumber, directDialControlSuffix));
dict.Add("email", string.Concat(genericPrefix, refNumber, emailDialControlSuffix));
return dict;
}
TryGetValue
call. There are various ways to fix the issue, of course (such as what CharlesNRice mentioned), but I can't help but wonder if the most streamlined version of this would be to just have one dictionary or static class with all the labels that you can reference. This strikes me as a "don't invent a fancy tool when all you need is a hammer" kind of situation. \$\endgroup\$