Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 681397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:28:44+00:00 2026-05-14T01:28:44+00:00

I have some logic that depends upon two properties being set, as it executes

  • 0

I have some logic that depends upon two properties being set, as it executes when both properties have a value. For example:

private void DoCalc() {
  if (string.IsNullOrEmpty(Property1) || string.IsNullOrEmpty(Property2))
    return;
  Property3 = Property1 + " " + Property2;
}

That code would need to be executed every time Property1 or Property2 changed, but I’m having trouble figuring out how to do it in a stylistically acceptable manner. Here are the choices as I see them:

1) Call method from ViewModel

I don’t have a problem with this conceptually, as the logic is still in the ViewModel – I’m not a ‘No code-behind’ nazi. However, the ‘trigger’ logic (when either property changes) is still in the UI layer, which I don’t love. The codebehind would look like this:

void ComboBox_Property1_SelectedItemChanged(object sender, RoutedEventArgs e) {
  viewModel.DoCalc();
}

2) Call method from Property Setter

This approach seems the most ‘pure’, but it also seems ugly, as if the logic is hidden. It would look like this:

public string Property1 {
  get {return property1;}
  set {
    if (property1 != value) {
      property1 = value;
      NotifyPropertyChanged("Property1");
      DoCalc();
    }
  }
}

3) Hook into the PropertyChanged event

I’m now thinking this might be the right approach, but it feels weird to hook into the property changed event in the implementing viewmodel. It would look something like this:

public ViewModel() {
  this.PropertyChanged += new PropertyChangedEventHandler(ViewModel_PropertyChanged);
}

void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
  if (e.PropertyName == "Property1" || e.PropertyName == "Property2") {
    DoCalc();
  }
}

So, my question is, if you were browsing through some source code with that requirement, which approach would you prefer to see implemented (and why?). Thanks for any input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:28:45+00:00Added an answer on May 14, 2026 at 1:28 am

    I don’t think doing it in the setter is ugly… actually it’s probably the best of the 3 approaches you mentioned, because when you read the code, you immediately see that changing the value of Property1 or Property2 will recompute Property3 ; this is not obvious at all with the 2 other approaches.

    However, I would use neither of these options. I think a better way of doing it would be to make Property3 read only, and compute its value in the getter, according to Property1 and Property2 :

    public string Property3
    {
        get { return Property3 = Property1 + " " + Property2; }
    }
    

    That way, in the setters of Property1 and Property2, you just need to call NotifyPropertyChanged for Property3 as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some custom logic that needs to be executed every single time a
In XCode 4.2, I have some logic tests that I want to exercise against
I have some logic in a method that operates on a specified type and
I have a library that I created with some business logic that includes writing
I am creating a series of UserControls that all have some similar business logic.
I have a LinkButton that has to postback to perform some logic. Once it
I have a HtmlHelper extension method that I would like to apply some logic
Let's say you have a business logic method that can perform some operation across
Given a base class where I have some logic that must run before and
I have some login code that works well for Firefox and other browsers, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.