简体   繁体   中英

C# (WPF) methods calling from different classses

I'm very new to coding. I have a problem when calling method from B to class A, when input variable involved. Please explain why a.PrintAfromB(); giving me 0 value. How to overcome the situation.? I see some examples use MVVM. But those are very complex for me in this stage. Seems like i'm getting the input variable incorrectly or calling methods incorrectly. I'm sucked and cannot move forward without resolving this.

Main

public partial class MainWindow : Window {
    public MainWindow(){
        InitializeComponent();
        this.DataContext = this;
    }

        B b = new B();
        A a = new A();

    private void Button_Click(object sender, RoutedEventArgs e) {
        b.Bat = double.Parse(one.Text);
        b.PrintB();
        a.PrintAfromB();
        a.PrintAfromBB();
    }
}

A

class A
{
    double Apple { get; set; }    
    B b1 = new B();           
    public void PrintAfromB() {

        Console.WriteLine("Calling method from B where input involved: "+b1.CallB());
    }

    public void PrintAfromBB() {

        Console.WriteLine("Calling method from B where input not involved: " + b1.CallBB());
    }
}

B

public class B{
   public double Bat { get; set; }
   public double k = 0;

    public void PrintB(){
        k = Bat;
        Console.WriteLine("test input value" +k);
    }

    public double CallB(){
        return 10 * Bat;
    }

    public double CallBB(){
        return 10 * 10;
    }
}

XAML

<Window x:Class="inputtest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:inputtest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBlock HorizontalAlignment="Left" Margin="62,141,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBox x:Name="one" HorizontalAlignment="Left" Height="23" Margin="198,134,0,0" TextWrapping="Wrap" Text="10" VerticalAlignment="Top" Width="120"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="380,263,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

</Grid>

The problem is A Class has another instance of B different from one in the Main.

To make A print something you entered, you should put B instance into A.

so make A like below:

class A
{
  B b1;
  public A(B b){
    b1 = b;
  }

  double Apple { get; set; }    

  public void PrintAfromB() {
    Console.WriteLine("Calling method from B where input involved: "+ b1.CallB());
  }

  public void PrintAfromBB() {
    Console.WriteLine("Calling method from B where input not involved: " + b1.CallBB());
  }
}

and then change Main like below:

public partial class MainWindow : Window {
  public MainWindow(){
    InitializeComponent();
    this.DataContext = this;
  }

  B b = new B();
  A a = new A(b);

  private void Button_Click(object sender, RoutedEventArgs e) {
      b.Bat = double.Parse(one.Text);
      b.PrintB();
      a.PrintAfromB();
      a.PrintAfromBB();
  }
}

hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM