2012-11-26 111 views
-1

下面是代碼,因爲它現在代表着我將包括程序的所有代碼,因爲我之前留下了一些位。由於你的幫助我已經改變了一些位,這些位用星號和/// 第一類是在直接編輯表單時從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; 
     } 
    } 

} 

此文件包含三個變量距離,緯度和經度。

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

    } 
+0

您是否嘗試過做它構造的一部分嗎? –

+0

通過這個,你是指將它包含在頂部的public form1()構造函數中?是的,我有,但我不知道該怎麼做,如果我把pathFile;在頂部,它只是以紅色突出顯示。 – user1744093

+0

您是否僅從Form1的代碼創建數據點?如果是這樣,你應該考慮康斯坦丁的答案。 –

回答

3

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 instanses。

var dp = new dataPoints(pathFile); 

但請記住,只有pathFileopenDataListToolStripMenuItem_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

+0

謝謝你的幫助。我只是想明白。 – user1744093

+0

@ user1744093如果不清楚 – horgh

+0

好吧,我通過創建consutructor p將對話框中的值傳遞給datapoints對象,現在當我將構造函數p放入它返回的streamreader中時一個錯誤說「值不能爲空」 – user1744093

相關問題