![](/img/trans.png)
[英]How to Store the returned variable from one class to another class variable?
[英]How to send a variable from one class to another
這是現在的代碼,在我之前遺漏了一些代碼的同時,我將包括該程序的所有代碼。 由於使用星號和///強調了您的幫助,我更改了一些位。第一類是在直接編輯表單時從Windows Forms創建的標准類。
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;
}
}
}
該文件包含三個變量距離,緯度和經度。
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; } }
}
你pathFile
是一個方法局部變量,所以它的不同之處在於方法(這里的身體inacccesible任何地方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
創建dataPoints
,或者將pathFile
設置為表單的字段,以便可以使用該表單的任何方法對其進行訪問。 然后,您將有機會以這種形式的任何方法訪問pathFile
。
根據您以前的帖子 ,它應該類似於:
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
}
}
PS:脫離主題,但請考慮閱讀《 MSDN名稱指南》
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.