Passing value to another class

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

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

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

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>

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

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,

Answers


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.


in your code, you confused between 2 "ip" Instances of class Input. the moments and curvature arrays in Input do not become NULL. What happens is that there are 2 different objects called ip: 1 inside the Button_click (that will be destroyed once this event finishes), and another ip that is created when the Window "PlotMPhi" instantiates the Class "MVMMPhi" which itself instantiates 2 things: a collection MeaMPhis, that will contain the many data pairs "MeaMPhi" and a new "ip" instance of Input which contains 2 empty arrays. Then your workaround was (cumbersome) passing the 2 arrays to PlotMPhi, which then had to pass them into MVMMPhi, so that it can use them to populate the data inside the collection MeaMPhis. Then iterating through the arrays is needed and is of course very costly.

What I include below contains 3 main changes: 1) I built the initial data from the file directly into a MeaMPhis collection of MeaMPhi. 2) I pass the collection (i.e. a reference to the instance MeaMPhis) to the window PlotMPhi, which then passes it down the same chain to MVMMPhi. 3) thanks to (1)+(2), MVMMPhi class no longer needs to do a copy from curvature[],moment[], to a collection of MeaMPhi. Its own MeaMPhis is points directly (a reference) to this collection which was already filled by the Button_click and was simply passed by ref to the MeaMPhi constructor.

Here is the slightly modified Button_Click

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Collection<MeaMPhi> MeaMPhis = new Collection<MeaMPhi>();

        //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++)
        {
            MeaMPhis.Add(new MeaMPhi
            {
                Curvature = Double.Parse(rows[i][9]),
                Moment = Double.Parse(rows[i][10])
            });

            //curvature[i] = Double.Parse(rows[i][9]);
            //moment[i] = Double.Parse(rows[i][10]);
        }
        //ip.curvature = curvature;
        //ip.moment = moment;

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

    }
}

and below is the window and the other classes.

public partial class PlotMPhi : Window
{
    public PlotMPhi(Collection<MeaMPhi> MeaMPhis)//double[] moment,double [] curvature)
    {
        var vm = new MVMMPhi(MeaMPhis);
        this.DataContext = vm;
        InitializeComponent();
    }

    private void SavePlot_Click(object sender, RoutedEventArgs e)
    {

    }

    private void exit_Click(object sender, RoutedEventArgs e)
    {

    }
}

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


    public MVMMPhi(Collection<MeaMPhi> Meamphis)
    {
        MeaMPhis = Meamphis;
        //Input ip = new Input();
        /*
        for (int i = 0; i < 40; i++)
        {

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

        }
         */
    }
}

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

}

Thank you for your complete code which helped me learn a bit about OxyPlot. I hope the full working code above clarifies my explanations. The primary point here was to avoid copying the data unnecessarily, and to avoid creating the 2 "ip" instances of Input(). BTW, in the proposed solution the Input() class is no longer used.


Need Your Help

Write contents into UTL_FILE immediately after a single iteration

oracle plsql utl-file

I have PL/SQL Block, it query from a Table function and I use a cursor to process it record by record, have some business logic and finally write the Qualified records into the file.

Should I compile release builds with debug info as "full" or "pdb-only"?

c# visual-studio debugging build debug-symbols

In Visual Studio 2010 for a C# project, if you go to Project Properties > Build > Advanced > Debug Info you have three options: none, full, or pdb-only. Based on the answer to this question, I bel...