0

学校向けに作成しようとしているプログラムで、少し問題が発生したようです。データを受信し、合計を取得して新しい画面に送信する新しいクラスを作成するとします。授業で行ったのと同じことをしましたが、違いは、それはテキスト ボックスからのものであり、今回は移動する必要があるデータ用のテキスト ボックスがないことです。そのため、試した方法ではエラーが発生しました。これが私がこれまでに得たものです:

    public StudentSelection()
    {
        InitializeComponent();
    }
    public decimal firstCounter;
    public decimal secondCounter;
    public decimal finalCounter;

    struct StudentScores
    {
        public string StuId;
        public decimal TestOne;
        public decimal TestTwo;
        public decimal Final;  
    }
    StudentScores[] allStudentScores = new StudentScores[10]; 
    // class level array to hold 10 products - read in from a file

    private void StudentSelection_Load(object sender, EventArgs e)
    {
        // read products file into products structure array
        try
        {


            // ALWAYS initialize a structure array before using the array
            //  before adding, changing, etc.
            for (int x = 0; x < 10; x++)
            {
                allStudentScores[x].StuId = "";
                allStudentScores[x].TestOne = 0;
                allStudentScores[x].TestTwo = 0;
                allStudentScores[x].Final = 0;
            }

            int arrayIndex = 0; // needed for incrementing index value of the array
            // when reading file into array

            // now read file into the array after initialization
            StreamReader inputFile;

            string lineofdata; // used to hold each line of data read in from the file
            string[] ProductStringArray = new string[4]; // 6 element string array to hold 
            // each "field" read from every line in file

            inputFile = File.OpenText("StudentScores.txt"); // open for reading

            while (!inputFile.EndOfStream) //keep reading until end of file
            {
                lineofdata = inputFile.ReadLine(); // ReadLine() reads an entire row of data from file
                ProductStringArray = lineofdata.Split(','); //each field is separated by ';'
                allStudentScores[arrayIndex].StuId = ProductStringArray[0]; // add first element of array to first column of allProducts
                allStudentScores[arrayIndex].TestOne = decimal.Parse(ProductStringArray[1]);
                firstCounter += allStudentScores[arrayIndex].TestOne;
                allStudentScores[arrayIndex].TestTwo = decimal.Parse(ProductStringArray[2]);
                secondCounter += allStudentScores[arrayIndex].TestTwo;
                allStudentScores[arrayIndex].Final = decimal.Parse(ProductStringArray[3]);
                finalCounter += allStudentScores[arrayIndex].Final;
                StudentListView.Items.Add(ProductStringArray[0]);
                arrayIndex++; // increment so NEXT row is updated with next read

            }

            //close the file
            inputFile.Close(); 
        }
        catch (Exception anError)
        {
            MessageBox.Show(anError.Message);

        }
    }
    private void NextButton_Click(object sender, EventArgs e)
    {
        decimal firstResult, secondResult, finalResult, stuOne, stuTwo, stuThree;
        string stuName;

        // call the method in our datatier class
        decimal.TryParse(firstCounter, out firstResult);
        decimal.TryParse(secondCounter, out secondResult);
        decimal.TryParse(finalCounter, out finalResult);

        DataTier.AddOurNumbers(firstResult, secondResult, finalResult);
        DataTier.StudentData(stuName, stuOne, stuTwo, stuThree);

        // now hide this window and display a third window with the total
        this.Hide();
        // display third window
        ScoreScreen aScoreScreen = new ScoreScreen();
        aScoreScreen.Show();
    }


}

そして私の新しいクラス

class DataTier
{
    // public static variable available to all windows
    public static decimal firstTotal, secondTotal, finalTotal, stuTestOne, stuTestTwo, stuTestThree;
    // static is not associated with any object

    public static string stuIDCode;

    // create a public method to access from all windows
    public static void AddOurNumbers(decimal NumOne, decimal NumTwo, decimal numThree)
    {
        // devide to get an average
        firstTotal = NumOne / 10;
        secondTotal = NumTwo / 10;
        finalTotal = numThree / 10;
    }

    public static void StudentData(string name, decimal testOne, decimal testTwo, decimal testThree)
    {
        stuIDCode = name;
        stuTestOne = testOne;
        stuTestTwo = testTwo;
        stuTestThree = testThree;
    }
}

エラーは 3 つの decimal.TryParse 部分にあります。「10 進数から文字列に変換できません」というエラー以外は、なぜ機能しないのかわかりません。どんな助けでも大歓迎です。

4

2 に答える 2

2

次から変更します。

    decimal.TryParse(firstCounter, out firstResult);
    decimal.TryParse(secondCounter, out secondResult);
    decimal.TryParse(finalCounter, out finalResult);

    DataTier.AddOurNumbers(firstResult, secondResult, finalResult);

に:

    DataTier.AddOurNumbers(firstCounter, secondCounter, finalCounter);

問題は、あなたがdecimal.TryParse(string s, out decimal result)decimal.TryParse(decimal s, out decimal result) として呼び出そうとしていることです。

あなたの入力はすでにdecimalあり、変換は必要ありません。


補足として、コードは

 decimal.TryParse(someString, out someOutputDecimal);

その周りに適切なif ステートメントがないと、サイレントに失敗します (失敗については何も通知しません)。実際、出力値は 0 に設定され、誤った入力が受信されなかったかのように動作します。入力が常に有効である必要がある場合は、decimal.Parse(someString)代わりに使用する必要があります。ただし、場合によっては、入力が無効な場合にデフォルトで 0 にすることが望ましい動作になることがあります。

于 2013-01-19T19:01:10.747 に答える
0

このdecimal.TryParse()メソッドは、最初の引数として文字列を受け取り、それをdecimal値に変換しようとします。あなたの場合、渡してTryParse()いる変数はすでにdecimal変数であり、それらを解析する必要はありません。クラス変数をローカル変数にコピーするだけの場合は、次のようにするだけです。

firstResult = firstCounter;
secondResult = secondCounter;
finalResult = finalCounter;

または、この特定のケースでは、クラス変数を直接 に渡すことができますAddOurNumbers:

DataTier.AddOurNumbers(firstCounter, secondCounter, finalCounter);

ここで注意すべきことの 1 つはdecimals、C# の やその他のプリミティブなどの値型は、ある値から別の値に代入したり、メソッドに渡したりするたびにコピーされることです。これはfirstCounter、 、secondCounter、またはの値が のthirdCounter呼び出し後に変化しDataTier.AddOurNumbers()ても、データ層が既に受け取っている値は変化しないことを意味します。

于 2013-01-19T19:02:50.043 に答える