Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置其他类的属性_C#_Class_Properties - Fatal编程技术网

C# 设置其他类的属性

C# 设置其他类的属性,c#,class,properties,C#,Class,Properties,我想指出的是,我对C语言完全陌生,我只是四处测试,以掌握这门语言 我想要实现的是在辅助窗口中控制台打印我在主窗口中设置的两个值 这是一个包含在MainWindow类中的函数,通过单击按钮触发 private void ValidationExecuted(object sender, ExecutedRoutedEventArgs eventArgs) { // If the validation was successful, let's open a new wind

我想指出的是,我对C语言完全陌生,我只是四处测试,以掌握这门语言

我想要实现的是在辅助窗口中控制台打印我在主窗口中设置的两个值

这是一个包含在MainWindow类中的函数,通过单击按钮触发

private void ValidationExecuted(object sender, ExecutedRoutedEventArgs eventArgs)
    {
        // If the validation was successful, let's open a new window.
        GeneratorWindow generatorWindow = new GeneratorWindow();
        generatorWindow.TextBlockName1.Text = this.tbPoints.Text;
        generatorWindow.TextBlockName2.Text = this.tbPDC.Text;

        int.TryParse(this.tbPoints.Text, out int numberOfPoints);
        int.TryParse(this.tbPDC.Text, out int pdc);

        // Those lines correctly print the values I've inserted in the TextBoxes.
        Console.WriteLine(numberOfPoints);
        Console.WriteLine(pdc);

        generatorWindow.NumberOfPoints = numberOfPoints;
        generatorWindow.MainPDC = pdc;
        generatorWindow.Show();

        // Resetting the UI.
        this.validator = new Validator();
        this.grid.DataContext = this.validator;
        eventArgs.Handled = true;
    }
现在,我的第二个窗口:

public partial class GeneratorWindow : Window
{
    /// <inheritdoc />
    /// <summary>
    /// Initializes a new instance of the <see cref="T:ABB_Rapid_Generator.GeneratorWindow" /> class.
    /// </summary>
    public GeneratorWindow()
    {
        this.InitializeComponent();
        // Those lines just print a pair of 0.
        Console.WriteLine(this.NumberOfPoints);
        Console.WriteLine(this.MainPDC);
    }

    /// <summary>
    /// Gets or sets the number of points.
    /// </summary>
    public int NumberOfPoints { private get; set; }

    /// <summary>
    /// Gets or sets the main PDC.
    /// </summary>
    public int MainPDC { private get; set; }
}
正如您在代码注释中看到的,主类中包含的Console.WriteLine工作正常。此外,我可以将自定义值分配给另一个类中包含的TextBlock。相反,secondary类中的Console.WriteLine行只输出几个零

我遗漏了什么?

问题在于,在GeneratorWindow中,您正在使用构造函数方法向控制台写入数据,因此在更改值之前会输出这些值

真正使输出工作的唯一方法是将值作为构造函数的参数传递,并在执行控制台输出之前在构造函数中设置它们。尽管似乎没有任何合乎逻辑的理由走这条路

例如:

public GeneratorWindow(int numberOfPoints, int mainPdc)
{
    this.InitializeComponent();

    this.NumberOfPoints = numberOfPoints;
    this.MainPDC = mainPdc;

    Console.WriteLine(this.NumberOfPoints);
    Console.WriteLine(this.MainPDC);
}
或者,如果希望在设置值后查看这些值,则需要将控制台输出移动到设置值后调用的另一个函数

例如,将此函数添加到GeneratorWindow:

在其他类中设置值后,可以调用该类:

GeneratorWindow generatorWindow = new GeneratorWindow();

generatorWindow.NumberOfPoints = numberOfPoints;
generatorWindow.MainPDC = pdc;

generatorWindow.OutputValues();

您可以添加一个参数ize构造函数来执行此操作

public partial class GeneratorWindow : Window
{
    //Private members
    int m_numberOfPoints;
    int m_mainPDC;

    /// <inheritdoc />
    /// <summary>
    /// Initializes a new instance of the <see cref="T:ABB_Rapid_Generator.GeneratorWindow" /> class.
    /// </summary>
    public GeneratorWindow(int mainPDC,int numberOfPoints)
    {
        this.InitializeComponent();
        this.m_mainPDC = mainPDC;
        this.m_numberOfPoints = numberOfPoints;
    }

    /// <summary>
    /// Gets or sets the number of points.
    /// </summary>
    public int NumberOfPoints 
    { 
      get{ return m_numberOfPoints; }
      set{ m_numberOfPoints = values; }
    }
    /// <summary>
    /// Gets or sets the main PDC.
    /// </summary>
    public int MainPDC
    { 
      get{ return m_mainPDC; }
      set{ m_mainPDC= values; }
    }

    public void Print()
    {
        Console.WriteLine(this.NumberOfPoints);
        Console.WriteLine(this.MainPDC);
    }
}
将辅助窗口调用更改为

 generatorWindow  = new GeneratorWindow(pdc,numberOfPoints);
 generatorWindow.Print();
此外,在IMO中,您的代码执行得不好,为什么要这样设置值

   generatorWindow.TextBlockName1.Text = this.tbPoints.Text;
   generatorWindow.TextBlockName2.Text = this.tbPDC.Text;
如果您像上面的示例一样设置了私有变量,那么您可以在同一个类中执行所有转换、打印和接收控制台输出


上面的答案是正确的,但还有另一种选择。
设置值后,可以使用setNumberOfPoints、setMainPDC和print co console等设置器方法。所以在ValidationExecuted中,您需要调用一个函数来设置变量,在该函数中设置变量后,您可以将其打印到控制台。但别忘了从构造函数中删除控制台打印

当GeneratorWindow创建时,2个值的默认值为0,当您更新它们时,您永远不会输出它们在GeneratorWindow打印中使用WriteLine或其他东西写入方法,在MainWondow更新了值后调用它。这篇文章似乎并没有为这个问题提供答案。请编辑您的答案,或者将其作为评论发布到另一个答案上。谢谢您的见解。@Davide3i很乐意提供帮助
 generatorWindow  = new GeneratorWindow(pdc,numberOfPoints);
 generatorWindow.Print();
   generatorWindow.TextBlockName1.Text = this.tbPoints.Text;
   generatorWindow.TextBlockName2.Text = this.tbPDC.Text;