0

これが以下のコードです。

nextButton ボタンをクリックすると、テキストファイルの次の 3 つの数字に循環するようにしようとしています。私は理解できません、私がここに持っているものはうまくいくはずです:[

namespace GPSProject
{
    class dataPoints
    {
        public int Count { get { return Points.Count; } }
        List<dataPoint> Points;
        //string p;

        public dataPoints(/*string path*/)
        {
            Points = new List<dataPoint>();
           // p = path;
            TextReader tr = new StreamReader(/*p*/"C:/Test.txt");
            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 GPSProject
{
    public partial class Form1 : Form
    {
        private int count;
        internal dataPoints myDataPoints;
        public Form1()
        {
            myDataPoints = new dataPoints();
            InitializeComponent();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            {




                 count++;
                    if (count == (myDataPoints.Count))
                    {
                        count = 0;
                    }



                dataPoint a = myDataPoints.getItem(count);
                textBoxLatitude.Text = a.CurLatitude;
                textBoxLongtitude.Text = a.CurLongtitude;
                textBoxElevation.Text = a.CurElevation;


            }
        }
    }
}

上記は Windows フォームの名前空間 GPSProject { class dataPoint { private string latitude; プライベート文字列の経度。プライベート ストリング エレベーション。

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

最後に、これは数値を保持するクラスです。テキストボックスに表示させようとしている数値は、CurLongtitude / Latitue / Elevationのサイクルです

4

1 に答える 1

0

最初に行うことは、データ用の適切な容器を作成することです: DataPoint エンティティ:

class DataPoint
{
    // Option 1: Field + read only property
    private string _latitude;
    public string Latitude { get { return _latitude; } }

    // Option 2: Property + compiler generated field
    public string Longitude { get; private set; }
    public string Elevation { get; private set; }

    // Constructor
    public DataPoint(string latitude, string longtitude, string elevation)
    {
        // Internally in this class we use fields
        _latitude = latitude;

        // Unless we use property option 2
        this.Longitude = longitude;
        this.Elevation = elevation; 
    }
}     

次に、静的メソッドを DataPoint クラスに追加して、ディスクからデータ ポイントをロードします。

public static List<DataPoint> LoadFromFile (string filename) 
{
    // The .NET framework has a lot of helper methods
    // be sure to check them out at MSDN

    // Read the contents of the file into a string array
    string[] lines = File.ReadAllLines(filename);

    // Create the result List
    List<DataPoint> result = new List<DataPoint>();

    // Parse the lines
    for (string line in lines)
    {
        string[] bits = line.Split(',');

        // We're using our own constructor here
        // Do watch out for invalid files, resulting in out-of-index Exceptions
        DataPoint dataPoint = new DataPoint(bits[0], bits[1], bits[2]);
        result.Add(dataPoint);
    }

    return result;
}

これで、すべての構成要素が揃いました。アプリケーションを作成しましょう:

public partial class Form1 : Form
{
    private int _index;
    private List<DataPoint> _dataPoints;

    public Form1()
    {
        // Since this is a simple test application we'll do the call here
        _dataPoints = DataPoint.LoadFromFile(@"C:\Test.txt");

        InitializeComponent();
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        // Cycle the data points
        _index++;
        if (_index == _dataPoints.Count)
        {
            _index = 0;
        }

        // Get the specific data point
        DataPoint dataPoint = _dataPoints[_index];

        // The empty texts are UI only, so we could check them here
        if (dataPoint.Latitude == null || dataPoint.Latitude == "")
        {
            textBoxLatitude.Text = "No Latitude Specified";
        }
        else
        {
            textBoxLatitude.Text = dataPoint.Latitude;
        }

        // A shorter, inline version
        textBoxLongtitude.Text = String.IsNullOrEmpty(dataPoint.Longitude) ? "No Longitude Specified" : dataPoint.Longitude;

        // Or if we don't care about empty texts
        textBoxElevation.Text = dataPoint.Elevation;
    }
}

もちろん、コードをさらに短くしたり、LINQ などの最新の手法を使用したりする方法はたくさんありますが、既存のコードから離れすぎないようにしています。私はコードを試していないので、SOでここに入力しました:)

また、コードのフォーマットにも注意してください。適切な大文字と小文字の区別と標準に従うことで、コードは他の人にとって非常に読みやすくなります。

MSDN には、.NET Framework クラスに関する多くの優れた例と広範なドキュメントがあります。

于 2012-11-28T01:57:07.360 に答える