Checking a large dictionary of given strings with user input

I have a profile that has a lot of custom settings, and I'm kind of dissatisfied with the good approach to validating what the user enters when submitting validation by mapping those values โ€‹โ€‹to object properties.

For example, I have a dictionary

public static Dictionary<string, string> objProfileSelections = new Dictionary<string, string>();

public static string MySelections(string key)
{
    objProfileSelections.Add("1", "No Answer");
    objProfileSelections.Add("3", "Less Than $25,000");
    objProfileSelections.Add("5", "$35,000 to $50,000");
    objProfileSelections.Add("7", "$50,000 to $75,000");
    objProfileSelections.Add("9", "$75,000 to $100,000");
    objProfileSelections.Add("11", "$100,000 to $150,000");
    objProfileSelections.Add("13", "$150,000+");
    objProfileSelections.Add("2", "No Answer");
    objProfileSelections.Add("4", "Less Than $25,000");
    objProfileSelections.Add("6", "$35,000 to $50,000");
    objProfileSelections.Add("8", "$50,000 to $75,000");
    objProfileSelections.Add("10", "$75,000 to $100,000");
    objProfileSelections.Add("12", "$100,000 to $150,000");
    objProfileSelections.Add("14", "$150,000+");
    string item;
    objProfileSelections.TryGetValue(key, out item);
    return item;
}

      

I like to pass a list of key strings from the user and pass those elements to populate the object. The problem is that I don't know how to code it so that it knows which property to go to, I was looking at thinking, but I couldn't find examples that have a set of value words that show up in the property names.

To make it clearer, when the user makes a selection, it goes as a parameter in the dictionary, and the dictionary outputs the items. From key 1, the value "No response" appears. If the user has selected all the checkboxes, this will be the value - (1,3,5,7,9,11,13). I need to retrieve these values โ€‹โ€‹when there is a matching key for a match property. For example, if a user clicks the 1.5 button but leaves it unoccupied, how do you know which user preferences are selected? How do I get the program to know which property will populate based on the results?

* edit some properties that I would like to display on

public string MyAnnualIncome{ get; set; }
public List<string> InterestAnnualIncome{ get; set; }

      

So, the first property will take one value, and the second property will take multiple values.

When the key matches the value, a dictionary is issued, I need the odd values โ€‹โ€‹going to MyAnnualIncome, and the even values โ€‹โ€‹going to InterestAnnualIncome.


  • so no one gets confused odd, and even keys are assigned to it, odd numbers belonging to a specific group of properties and even ones belonging to another, based on the selected html elements (even being my choice, odd is what I'm interested in)

* Update Is there a way that I can use keys like 1,3,5 and pass them to the list using the except extension method. Then take the results and use a method to convert values โ€‹โ€‹from the listed datatypes to strings?

+3


source to share


2 answers


Hope I understood your question. I would add a small helper class (this is a solution that doesn't use reflection, but uses delegates instead):

public class PropertyModifier
{
   private string text;
   private Func<string> modifier;

   public PropertyModifier(Func<string> modifier)
   {
       this.modifier = modifier;
   }

   public PropertyModifier With(string text)
   {
       PropertyModifier newModifier = new PropertyModifier(modifier);
       newModifier.text = text;
       return newModifier;
   }

   public void Modify()
   {
       modifier(Text);
   }
}

      

Then I would rewrite your code and map the dictionary of this class instead:



public static Dictionary<string, PropertyModifier> objProfileSelections = new Dictionary<string, PropertyModifier>();

public static MyUserProfile Profile; //Assuming this is the object you want to modify

public static string MySelections(string key)
{
    PropertyModifier myIncome = new PropertyModifier(text => Profile.MyAnnualIncome = text);
    PropertyModifier interestIncome = new PropertyModifier(text => Profile.InterestAnnualIncome.Add(text)); 

    objProfileSelections.Add("1", myIncome.With("No Answer"));
    objProfileSelections.Add("3", myIncome.With("Less Than $25,000"));
    ...
    objProfileSelections.Add("2", interestIncome.With("No Answer"));
    objProfileSelections.Add("4", interestIncome.With("Less Than $25,000"));
    ...
}

      

Then, when processing the custom selection, get the associated PropertyModifier from the dictionary and call its method Modify

.

+1


source


I tried in this code to illustrate how you can change properties of different classes that can make up a profile. Modifications are only done by reflection, i.e. By simply specifying the class name, the property name that will change in each class, and the string value that should be assigned to the property.

Not sure if this meets your expectations :(



Profile profile = new Profile() ;
profile.SetPropertyValue("hair","color","brown") ;        

internal class Profile()
{
  private Hair hair_ = new Hair();   
  private Job  job_  = new Job ();

  internal Hair hair { get { return hair_ ; } } 
  internal Job  job  { get { return job_  ; } } 

  private void SetPropertyValue(string profileItemName, string ItemPropertyName, string value)
  { // it is assumed that the different items (hair or job) of the Profile are accessible 
    // with a a property

    // first find the Item object, i.e. hair or job                  
    object itemObj = this.GetType().GetProperty(profileItemName).GetValue(this,null);
    // assign to Item property the input value, e.g. hair.color=Brown
    itemObj.GetType().GetProperty(ItemPropertyName).SetValue(itemObj, value, null);
   }
 }

internal class Hair()
{
  private string color_ ;   
  private string style_ ;

  internal string   color { get { return color_  ; } set {color_ = value ; } } 
  internal string   style { get { return style_  ; } set {style_ = value ; } } 
 }

      

0


source







All Articles