如何将变量从一个类发送到另一个类

时间:2012-11-26 23:31:40

标签: c# .net forms class

这是现在的代码,我将包含该程序的所有代码,因为我之前留下了一些内容。由于你的帮助我改变了我用星号和///强调的位数 第一个类是直接编辑表单时从Windows窗体创建的标准类。

namespace DistanceEstimatorFinal
{
    public partial class Form1 : Form
    {
        private bool saved;

        public Form1()
        {

            dataPoints mydataPoints = new dataPoints();
            InitializeComponent();
            dataPoint a = mydataPoints.getItem(0);
            latTextBox.Text = a.CurLatitude;
            longTextbox.Text = a.CurLongtitude;
            eleTextBox.Text = a.CurElevation;
            saved = true;

        }             

        private void latTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void openDataListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "CSV files (*.csv)|*.csv|Text files ( *.txt)|*.txt |All files (*.*)|*.*";
            if (ofd.ShowDialog(this).Equals(DialogResult.OK))
            {
                *var dp = new dataPoints (ofd.FileName);* /////

            }
        }       



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (saved)
            {
                if (MessageBox.Show("Save?", "Data Not Saved", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.ShowDialog();

                }

            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd1 = new SaveFileDialog();
            sfd1.Filter = "CSV files (*.csv)|*.csv|Text files ( *.txt)|*.txt |All files (*.*)|*.*";
            sfd1.ShowDialog();
        }

    }
}     

这个类旨在读取文件中的数据,我目前正在调整它以从open函数中读取文件。

namespace DistanceEstimatorFinal
{    
    public class dataPoints
    {              
        List<dataPoint> Points;
        string p;

        public dataPoints(string path)
        {
            p = path;
            Points = new List<dataPoint>();

            StreamReader tr = new StreamReader(p);

            string input;
            while ((input = tr.ReadLine()) != null)
            {
                string[] bits = input.Split(',');
                dataPoint a = new dataPoint(bits[0],bits[1],bits[2]);              
                Points.Add(a);  


            }

            tr.Close();
        }





        internal dataPoint getItem(int p)
        {
            if (p < Points.Count)
            {
                return Points[p];
            }
            else
                return null;
        }
    }

}

此文件包含三个变量Distance,latitude和Longtitude。

namespace DistanceEstimatorFinal
{
    class dataPoint
    {
        private string latitude;
        private string longtitude;
        private string elevation;

        public dataPoint()                               //Overloaded incase no value available
        {
            latitude = "No Latitude Specified";
            longtitude = "No Longtitude Specified";
            elevation = "No Elevation Specified";

        }

        public dataPoint(string Latitude, string Longtitude, string Elevation)
        {

            // TODO: Complete member initialization
            this.latitude = Latitude;
            this.longtitude = Longtitude;
            this.elevation = Elevation;

        }

        public string CurLongtitude { get { return this.longtitude; } }
        public string CurLatitude { get { return this.latitude; } }
        public string CurElevation { get { return this.elevation; } }

    }

1 个答案:

答案 0 :(得分:3)

您的pathFile是方法局部变量,因此除了该方法的主体(此处为openDataListToolStripMenuItem_Click)之外,它在任何地方都不可用。

您可以向dataPoints构造函数添加一个参数,以将该值传递给类:

public class dataPoints
{
    List<dataPoint> Points;
    public dataPoints(string path)
    {
        Points = new List<dataPoint>();
        //here `path` from constructor arguments
        TextReader tr = new StreamReader(path); 
        //...rest part of your code
    }

此外,您必须将值传递给此构造函数。您没有显示代码,您必须创建dataPoints个实例。

var dp = new dataPoints(pathFile);

但请记住,pathFile只能在openDataListToolStripMenuItem_Click中访问。因此,您应该在那里创建dataPoints,或者使pathFile表单的字段可以在该表单的任何方法中访问。然后,您将有机会以此表单的任何方法访问pathFile


根据your previous post,这应该是:

private void openDataListToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "CSV files (*.csv)|*.csv|Text files ( *.txt)|*.txt |All files (*.*)|*.*";
    if (ofd.ShowDialog(this).Equals(DialogResult.OK))
    {            
        //actually you don't even need to have a separate `pathFile` variable
        //just pass the value from the dialog straight to your `dataPoints` object
        var dp = new dataPoints(ofd.FileName);
        //...rest of your code
    }
}

P.S。:偏离主题,但请考虑阅读MSDN Guidelines for Names