2017-12-15 4 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

,363,210
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' 코드를 처음 보았을 때 제공 한 것입니다. 변수를 만들 때 좀 더 의미있는 이름을 사용 하시길 권합니다. 무료/온라인 Google'C# Basics Tutorial'을 읽고/이해합니다. 'Class Section'에 중점을두고 디버거를 사용하고, 중단 점을 설정하고,이 코드를 통해 스테핑을 시작하는 방법을 배웁니다. WPF는 고전적인 winforms로 시작하십시오. 초보자를 위해 좀 더 진보 된 것입니다. – MethodMan

+0

제안을 주셔서 대단히 감사합니다. :) – Yikes

답변

1

문제 클래스 메인 한 다른 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(); 
    } 
} 

당신은

그래서 같은 A 아래 만들 A.에 B 인스턴스를 놓아야합니다, 당신이 입력 인쇄 뭔가를 만들려면

도움이 되길 바랍니다.

+0

답장을 보내 주셔서 감사합니다. 귀하가 제안한 코드를 변경하려고 시도했습니다. 그러나 기본 클래스에서 매개 변수 "b"를 입력했습니다. 그것은 나에게 오류를 준다. ** 필드 이니셜 라이저가 비 정적 필드 메서드 또는 속성 생성 객체를 참조 할 수 없습니다 ** – Yikes

+0

버튼 내부에서 객체 생성을 시도하고 오류가 발생했습니다. 나는 그렇게하는 것이 적절하다는 것을 모른다. BTW, 작동하지 않습니다. 감사 – Yikes

관련 문제