2017-12-15 13 views
1

私は非常にコーディングすることができます。入力変数が関わっているときに、メソッドをBからクラスAに呼び出すときに問題があります。なぜa.PrintAfromB();私に0値を与える。どのように状況を克服する? MVVMを使用する例があります。しかし、これらは私にとってこの段階では非常に複雑です。私は間違って入力変数を取得しているか、またはメソッドを間違って呼び出しているようです。私は吸い込まれており、これを解決せずに前進することはできません。異なるクラスから呼び出すC#(WPF)メソッド

メイン

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(); 
    } 
} 

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> 

+0

あなたのコード 'YIKES'を最初に見たときの画面名は、変数を作成するときにもっと意味のある名前を使用することをお勧めします。無料のオンライン' C#Basics Tutorial'を読んで/ 'クラスセクション'に焦点を当て、私はデバッガの使い方、ブレークポイントの設定、そしてこのコードの実行を開始する方法も学びます。 WPFは初心者のために少し進んでいます – MethodMan

+0

提案をいただき、ありがとうございました。 :) – Yikes

答えて

1

問題は、クラスメイン内の1つの異なるBの別のインスタンスを有しています。入力した印刷何かを作るために

、あなたはそう下記のような作りA.

にBのインスタンスを置く必要があります。その後、

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()); 
    } 
} 

と以下のようにメインを変更:

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(); 
    } 
} 

助けて欲しい。

+0

返事をありがとう、私は示唆したコードを変更しようとしました。 しかし、メインクラスでは、パラメータ "b"を入力しました。それは私にエラーを与える。 **フィールドイニシャライザは非静的フィールドメソッドまたはオブジェクトを作成するプロパティを参照できません** – Yikes

+0

ボタン内にオブジェクトを作成しようとしましたが、エラーが消えました。私はそれをするのが適切であるかどうかわかりません。ところで、働いていない。ありがとう – Yikes

関連する問題