Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been given the criteria of dynamically populating values on a web page based on values the user saved.

This gist has the basics of the code.

What I'm wondering is if anyone can suggest a better method than what I've done.
The code is also used to read the values of the controls dynamically, hence the bad naming convention for the _getOrSet... method.

share|improve this question

closed as off-topic by Jamal Feb 21 '14 at 17:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must include the code you want reviewed. Code must not appear only in a link to an external source. Doing so makes us dependent on a third-party site and makes it harder to review your code. If your code is very large, please select only the portions in which you are especially interested for a review. You are welcome to keep the link to the rest of your code." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

You should include the code here, not just link to it. – svick Dec 27 '12 at 23:14
How dynamic are the controls on the page? Do the controls available on the page change as well, or is it only the data that changes? If only the data changes, you should look into creating an object to represent what is displayed and then data binding its properties to the controls on the page. – Dan Lyons Dec 31 '12 at 18:11

I would create a Facade class to manage the details of setting the control value. Something like this:

public abstract class SettableControl
    public abstract object Value {get;set;}
    //maybe some other properties, like Name

Then create an implementation of this for each of your control types. For example:

public TextBoxSettableControl
     private readonly TextBox _textBox;

     public TextBoxsettableControl(Textbox textBox)
          _textbox = textBox;

     public object Value
          { get { return _textBox.Text; } }
          { set { _textBox.Text = value.ToString(); } }

Then you set or get in a real natural and readable way against a consistent interface.

EDIT: you will probably want a factory for SettableControl that takes a Control as an input. This is where you switch statement will move to.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.