0

I have some issue with accessing classes. the issue is as below.

public class A1ViewModel
{
  int A1no;

  public int A1NO 
 {
    get { return A1no;}
    set { A1no=value;
          propertychanged("A1NO");
          M1()
        }
  }
  public M1()
  {
     // do some thing
  }
}

public class A2ViewModel
{
  int A2no;

    public int A2NO 
 {
    get { return A2no;}
    set { A2no=value;
          propertychanged("A2NO");
          M2()
        }
  }
  public M2()
  {
     //do some thing
  }
}

public class mainAViewModel()
{
  var a1ViewModel = new A1ViewModel();
  var a2ViewModel = new A2ViewModel();
}

Here A1ViewModel is viewmodel for one usercontrol and A2ViewModel is viewmodel for another usercontrol. and mainAViewModel is main ViewModel. in mainAViewModel. i have this two(A1,A2) viewmodels. the question is, if any changes happends in A1 viewmodel then i have to update A2 viewmodel property. Please gudie me. how i can achive this. Thanks in advance

0

2 Answers 2

2

I think what you're trying to do would involve the use of derived classes:

public class A1 : A2
{
  int A1no;

  public M1()
  {
     this.M2();
  }
}

public class A2
{
  int A2no;

  protected M2()
  { 
     // Do something
  }
}

public class mainA()
{
  var a1 = new A1();
  var a2 = new A2();

  a1.M1();
}

This way, A1 inherits from its base class A2. The only way an object of type A1 can access the A2.M2 method, however, is with the public or protected access modifier. If you don't put any access modifier before the method declaration, it defaults to private.

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

6 Comments

I am sorry that question is not given propertly. here A1 is viewmodel for one usercontrol and A2 is viewmodel for another usercontrol. and mainA is main ViewModel. in mainViewModel. i have this two(A1,A2) viewmodels. the question is if any changes happends in A1 viewmodel then i have to update A2 viewmodel property. Please gudie me. Thanks in advance....
Are you wanting to check if the user has changed the value of the field in the post action in your controller? Is A2 a hidden field or something?
I'm fairly sure he just wants something to happen in the A2 viewmodel when something changes in the A1 viewmodel. It is a fairly common problem that many of the MVVM frameworks have already elegantly solved. Have a look at my answer post,
Oh.. so, like, client side instead of server side?
That would be my guess, like a property in A2 whose value is dependent on a value in A1. But if that is the case he may be better off doing something in the model layer instead
|
0

Some of the MVVM frameworks achieve this by using a Mediator. Have a look at how the CinchV2 framework uses it. For example:

Use the following to listen for the change in your A2 viewmodel

Mediator.Instance.RegisterHandler<int>(MessageKeys.AgreementVersionChanged, OnAgreementVersionChanged);

And use the following to send the change in your A1 viewmodel

Mediator.Instance.NotifyColleagues(MessageKeys.AgreementVersionChanged, AgreementModel.AgreementId);

the awesome thing about the mediator in the CinchV2 framework is that you can just grab that part of it out, you do not have to implement the full CinchV2 framework.

2 Comments

Is there any possibility of doing this only using oops concepts?
Well yeh, it should be possible. That's what the other poster was basically showing you. If you are going to religiously stick to pure OOP though I would not like to have to maintain your system.

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.