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

错误出现在小数点后三位。TryParse 部分,我不知道为什么它不起作用,除了错误说“无法从十进制转换为字符串”。任何帮助将不胜感激。

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

这里要注意的一件事是,值类型(例如decimalsC# 中的其他原语)在您将它们从一个值分配给另一个值或将它们传递给方法时都会被复制。这意味着即使调用 后 、 或 的值发生变化firstCountersecondCounter您的数据层已经收到的值也不会发生变化。thirdCounterDataTier.AddOurNumbers()

于 2013-01-19T19:02:50.043 回答