Dictionary to generate control names 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&lt;string, string&gt; myControls1 = GeneratedControlNames(1);
        Dictionary&lt;string, string&gt; 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&lt;string, string&gt; GeneratedControlNames(int refNumber)
    {
        var genericPrefix = "coolControl";
        var departmentControlSuffix = "Department";
        var jobTitleControlSuffix = "Title";
        var directDialControlSuffix = "DDI";
        var emailDialControlSuffix = "Email";

        var dict = new Dictionary&lt;string, string&gt;();
        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;
    }
I think the dictionary is overkill.  Plus adds a lot more code with the TryGet.  I would make a class to help and hide the magic strings.  

As a side note magic strings should be made into constants so they are maintained in one place and if a typo you just need to fix it in one place.    Also having a 1 or 2 being passed in also seems magic to me. I would make that into an enum.  If we left it as an int then when coming back I have no idea what values I can pass in.  Is negative numbers allowed?  What about 9999? 

public enum RefNumber
{
    One = 1,
    Two = 2,
}


I'd create a class to hold the magic strings 

public static class ControlsNames
{
    private const string GenericPrefix = "coolControl";
    private const string DepartmentSufix = "Department";
    private const string JobTitleControlSuffix = "JobTitle";
    private const string DirectDialControlSuffix = "DDI";
    private const string EmailDialControlSuffix = "Email";


Looking at your example you always format with the Prefix then refnumber then optional suffix.  I'll make a method in the class to handle that for us

private static string FormatName(string suffix, RefNumber refNumber)
{
     return string.Concat(GenericPrefix, (int)refNumber, suffix);
}


Now we just need to make an easy way to know what suffix we need. I'm going to make some static methods that will do that for us and return back the string.  The only exception is Generic we don't want a suffix but we can just pass in string.empty

public static string Generic(RefNumber refNumber)
{
    return FormatName(string.Empty, refNumber);
}

public static string Department(RefNumber refNumber)
{
    return FormatName(DepartmentSufix, refNumber);
}

public static string JobTitle(RefNumber refNumber)
{
    return FormatName(JobTitleControlSuffix, refNumber);
}

public static string DirectDial(RefNumber refNumber)
{
    return FormatName(DirectDialControlSuffix, refNumber);
}

public static string Email(RefNumber refNumber)
{
    return FormatName(EmailDialControlSuffix, refNumber);
}


Now we don't have any dictionary or TryGets and the code looks like this now

public void DoStuffWithControls()
{
    string genericControl1 = ControlsNames.Generic(RefNumber.One);
    string genericControl2 = ControlsNames.Generic(RefNumber.Two);
    string departmentControl1 = ControlsNames.Department(RefNumber.One);;
    string departmentControl2 = ControlsNames.Department(RefNumber.Two); ;

    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";
}