将值传递给另一个类 [英] Passing value to another class

查看:92
本文介绍了将值传递给另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向您学习如何传递值.我有两组数组:矩数组和曲率数组.通过单击 MainWindow 类中的按钮从文本文件中读取每个数组,如下所示.

Hi I would like to learn from you how to pass the values. I have two sets of arrays: moment array and curvature array. Each array is read from textfile by clicking a button in MainWindow Class as shown below.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Input ip = new Input();

        double[] curvature = new double[40];
        double[] moment = new double[40];

        string path;
        path = "C:\\Desktop\\48co.out";
        string[] records = File.ReadAllLines(path);
        int linenumber = 0;
        string[][] rows = new string[40][];

        for (int i = 0; i < records.Length; i++)
        {
            if (records[i] == "step epscmax   in.    Tens.  Comp.  Comp. Tens.   force   force  rad/in  (K-ft)")
            { 
                linenumber = i + 1; 
            }
        }

        for (int i = linenumber; i < linenumber + 40; i++)
        {
            rows[i - linenumber] = records[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        }

        for (int i = 0; i < 40; i++)
        {
            curvature[i] = Double.Parse(rows[i][9]);
            moment[i] = Double.Parse(rows[i][10]);
        }

        ip.curvature = curvature;
        ip.moment = moment;

        PlotMPhi plotmphi = new PlotMPhi();
        plotmphi.Show();
    }
}

然后将这两个数组传递给另一个名为Input"的类.

And these two arrays are passed to another class called "Input".

class Input
{
    public Input()
    {
        double[] curvature = new double[40];
        double[] moment = new double[40];
    }

    public double[] curvature { get; set; }
    public double[] moment { get; set; }
}

我的想法是将所有输入参数存储在 Input 类中,如果其他类中的任何方法需要它们,可以将它们传递给.在这种情况下,我想使用 WPF 中名为 PlotMPhi 的 OXYplot 绘制带有曲率数组中的 x 点和矩数组中的 y 点的图表.

My idea was to store all the input parameters in Input class and if any methods in other classes need them, they can be passed to. In this case, I wanted to plot the chart with x points from curvature array and y points from moment array using OXYplot in WPF called PlotMPhi.

public partial class PlotMPhi : Window
{
    public PlotMPhi()
    {
        var vm = new MVMMPhi();
        this.DataContext = vm;
        InitializeComponent();
    }
 }

class MVMMPhi
{
    public Collection<MeaMPhi> MeaMPhis { get; private set; }

    public MVMMPhi()
    {
        MeaMPhis = new Collection<MeaMPhi>();
        Input ip = new Input();

        for (int i = 0; i < 40; i++)
        {
            MeaMPhis.Add(new MeaMPhi { Curvature = ip.curvature[i],Moment = ip.moment[i]});
        }
    }
}

public class MeaMPhi
{
    public double Curvature { get; set; }
    public double Moment { get; set; }
}

这是用于绘制图表的 xaml 文件.

Here is xaml file for plotting chart.

<Window x:Class="XsectionWIN.PlotMPhi"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf" 
    Title="Moment-Curvature" Height="480" Width="640">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="Save plot..." Click="SavePlot_Click"/>
                <Separator />
                <MenuItem Header="_Exit" Click="exit_Click" />
            </MenuItem>
        </Menu>
        <oxy:Plot Grid.Row="1" x:Name="plot" Title="Moment-Curvature" Subtitle="{Binding Subtitle}" >
            <oxy:Plot.Axes>
                <oxy:LinearAxis Position="Bottom" Title="Curvature (rad/in)" TitleFont="Arial" TitleFontSize="12" TitleColor="Black"/>
                <oxy:LinearAxis Position="Left" Title="Momennt (kips-in)" TitleFont="Arial" TitleFontSize="12" TitleColor="Black"/>
            </oxy:Plot.Axes>
            <oxy:Plot.Series>
                <oxy:LineSeries Title="M-curvature" DataFieldX="Period_t" DataFieldY="Acc_t" Color="Red"  StrokeThickness="3" ItemsSource="{Binding MeaMPhis}"/>

            </oxy:Plot.Series>
        </oxy:Plot>
    </Grid>
</Window>

问题是这两个数组没有传递给 MVMMPhi 类.我按 F11 键一步一步检查了程序.似乎这些数组被传递给 Input 类直到到达

The problem is these two arrays are not passed to MVMMPhi class. I checked the procedure step by step clicking F11 key. It seems these arrays are passed to Input class until reaching

PlotMPhi plotmphi = new PlotMPhi();
plotmphi.Show();

一旦进入这一步,Inout中的矩和曲率数组就变成NULL了.我之前遇到过这个,所以我所做的是直接将这些数组放入方法中,即

Once get into this step, moments and curvature arrays in Inout become NULL. I encountered this before so what I did was to put these arrays in the method directly i.e.

PlotMPhi plotmphi = new PlotMPhi(moment,curvature);

到目前为止,这对我有用.但是我稍后需要处理很多数组,所以我正在寻找简单的方法来处理并且想知道为什么我的想法不起作用.我是编程世界的新手.我不想用勺子喂食,所以任何建议或提示将不胜感激.如果您需要其他信息,请告诉我.感谢您的时间和帮助,

This has worked for me until now. But I need to handle lot of arrays later so I am looking for easy way to handle and would like know why my idea does not work. I am new in programming world. I don't want to get spoon-fed so any suggestions or tips will be appreciated. If you need additional info, please let me know. Thanks for your time and help,

推荐答案

DataContext 类的构造函数可以采用 Input 类型的参数,这样你就可以更改代码

The constructor for your DataContext class could be made to take a parameter of type Input so you would change your code thus

public MVMMPhi(Input data)
{
    MeaMPhis = new Collection<MeaMPhi>();

    for (int i = 0; i < 40; i++)
    {

            MeaMPhis.Add(new MeaMPhi
                                 {
                                     Curvature = data.curvature[i],
                                     Moment = data.moment[i]
                                 });

    }
}

这可能无法解决您的所有问题,但可能会让您的思考更直接一些.

This may not solve all your problems but it might make thinks a little more direct.

这篇关于将值传递给另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆