3

When I'm making user controls for both Winforms and ASP.NET, I find myself writing the same section of code over and over again for each property on the form, which is this:

private string _url;

public string Url
{
  get { return _url; }
  set
  {
    if (_url == value)
      return;

    _url = value;
    OnUrlChange(EventArgs.Empty);
  }
}

public event EventHandler UrlChanged;

protected virtual void OnUrlChange(EventArgs e)
{
  //Maybe add some extra logic here

  if(UrlChanged != null)
    UrlChanged(this, e);
}

Sometimes there's the odd change. Maybe I'll writing my own class that derives from EventArgs and use that.

This seems like it must be a common task. Maybe I'm doing it wrong and there's a much easier way to write the lot? But if not, are there any custom refactoring tools that I can set up to fill in this code given the Property name Url?

3
  • I'm not a .NET guy and not sure if I understand you, but I'm guessing your intent is to notify another element (perhaps something on the UI) every time you update a property (e.g. calling the set* method). You are right in thinking this is very common task, and rather than putting effort into creating a parent class or code snippet generator, I would look to see where this behavior is already implemented in the API, because I'm sure it is. Commented Nov 15, 2011 at 21:47
  • I would've thought so. Most controls in the .NET framework have this, or very similar code built into them. There are a few different implementations though that would make the behavior different so dumming it down much further than this would take away some of the flexibility. Commented Nov 15, 2011 at 21:53
  • Unfortunately, all the .NET library code I've ever decompiled follows the long-hand pattern, and I'm pretty sure there aren't any built-in solutions. @user617090: Do you use a platform that does have this functionality? How does it look? Commented Nov 15, 2011 at 22:51

4 Answers 4

2

Your pattern is pretty common, and is pretty much the best way to do this.

That being said, you should definitely use Visual Studio Snippets. They allow for easily creating templates for code like this. You can define your own placeholders, and when you insert the snippet, Visual Studio highlights the placeholders and allows you to tab through them.

I'd also recommend the Snippet Designer plugin, it will make snippet creation much easier and more fun. It also helps to find out snippet features that you didn't know existed.

Sign up to request clarification or add additional context in comments.

Comments

2

I had thought about this some couple of months ago and toyed with using this:

    public event PropertyChangedEventHandler PropertyChanged;

    int _MyProperty;
    public int MyProperty
    {
        get { return _MyProperty; }
        set { ChangeIfUnequal(ref _MyProperty, value, "MyProperty"); }
    }

    int _AnotherProperty;
    public int AnotherProperty
    {
        get { return _AnotherProperty; }
        set { ChangeIfUnequal(ref _AnotherProperty, value); }
    }


    void ChangeIfUnequal<T>(ref T Val, T NewValue, string Identifier="")
    {
        if (!Val.Equals(NewValue))
        {
            Val = NewValue;
            var temp = PropertyChanged;
            if(temp != null)
                temp(this, new PropertyChangedEventArgs(Identifier));
        }
    }

But I never ran it in production and haven't spent enough time to think if this is a marginal improvement or unnecessary complication. Undoubtedly it's not as flexible as explicitly setting it in each property as this would only raise on !equals. Take it or leave it I guess.

2 Comments

This is a good answer, because it is pretty close to what the OP is looking for. However, it also sheds light on the fact that the solution is not much better than the problem.
Quality answer. +1, but I've accepted the snippet method because thats the way I did it in the end. Definitely the most interesting answer though. If I could +10 I would :P
1

are there any custom refactoring tools that I can set up to fill in this code given the Property name Url?

You can create your own code snippet to do exactly that.

2 Comments

Oh I say! I didn't realise you could roll your own code snippets. Always thought they were Microsoft standard things. Every day's a school day.
@ConnellWatkins - Visual Studio is incredibly extensible.
0

It depends on your architecture, but if you're trying to reduce redundancy you could create a base class that contains any shared properties and methods, and have your controls inherit from it.

1 Comment

It's the pattern I'd like to be able to repeat, not the actual methods and properties. For example I'd like to be able to do the same thing somewhere else but with a Text property, _text field, a TextChanged event and an OnTextChanged method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.