【问题标题】:Setting properties from another class从另一个类设置属性
【发布时间】:2018-06-12 07:06:08
【问题描述】:

我想指出我对 C# 完全陌生,我只是在测试以掌握这种语言。

我想要实现的只是在辅助窗口中控制台打印我在 MainWindow 中设置的几个值。

这是 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; }
}

正如您在代码 cmets 中看到的,主类中包含的 Console.WriteLine() 工作正常。此外,我可以将我的自定义值分配给另一个类中包含的 TextBlocks。相反,二级类中的Console.WriteLine() 行只是输出了几个零。

我错过了什么?

【问题讨论】:

  • 生成 GeneratorWindow 后,这两个值将默认为 0,当您更新它们时,您永远不会输出它们
  • 用WriteLine()之类的在GeneratorWindow Print()中写一个方法,在MainWondow的值更新后调用它。

标签: c# class properties


【解决方案1】:

问题在于,在GeneratorWindow 中,您在 constructor 方法中写入控制台,因此在更改值之前输出这些值。。 p>

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

例如:

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

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

    Console.WriteLine(this.NumberOfPoints);
    Console.WriteLine(this.MainPDC);
}

或者,如果您想在设置值后查看这些值,则需要将控制台输出移动到您在设置值之后调用的另一个函数。

例如,将此函数添加到GeneratorWindow

public void OutputValues()
{
    Console.WriteLine(this.NumberOfPoints);
    Console.WriteLine(this.MainPDC);
}

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

GeneratorWindow generatorWindow = new GeneratorWindow();

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

generatorWindow.OutputValues();

【讨论】:

    【解决方案2】:

    你可以添加一个参数化构造函数来做到这一点

    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 generatorWindow = new GeneratorWindow();//here
    

    将二级窗口调用改为

     generatorWindow  = new GeneratorWindow(pdc,numberOfPoints);
     generatorWindow.Print();
    

    另外,您的代码在 IMO 中做得不好,为什么要设置这样的值?

       generatorWindow.TextBlockName1.Text = this.tbPoints.Text;
       generatorWindow.TextBlockName2.Text = this.tbPDC.Text;
    

    如果您像上面的示例一样设置了私有变量,您可以在同一个类中执行所有转换、打印和接收控制台输出。您只需调用构造函数和打印方法。

    【讨论】:

    • 感谢您的洞察力。
    • @Davide3i 很高兴为您提供帮助
    【解决方案3】:

    上面的答案是正确的,但还有另一种选择。 您可以使用诸如setNumberOfPointssetMainPDC 之类的setter 方法,并在设置值后打印co 控制台。因此,在ValidationExecuted 中,您调用一个函数来设置变量,并在设置变量后在该函数中将其打印到控制台。但是不要忘记从constructor删除打印到控制台

    【讨论】:

    • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对另一个答案的评论发布)。
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多