移动信息时遇到问题

时间:2013-01-19 18:56:16

标签: c#

我似乎在试图为学校制作的节目中遇到了一些障碍。我想创建一个新类来接收数据并获得总数以发送到新屏幕。好吧,我做了同样的事情,我们在课堂上做了,但不同的是,这是来自文本框,这次我需要移动的数据没有文本框,所以我尝试的方式给了我一个错误。这是我到目前为止所得到的:

    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部分,我不知道为什么它不工作,除了错误说“不能从十进制转换为字符串”。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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(十进制s,十进制结果)。

您的输入已为decimal,无需任何转化。


旁注是代码

 decimal.TryParse(someString, out someOutputDecimal);

没有正确的 if语句会无声地失败(它不会通知任何有关失败的信息)。事实上,输出值设置为0,就好像没有收到错误输入一样。如果输入始终有效,则应使用decimal.Parse(someString)。但是,如果输入无效,在某些情况下默认为0,可能是所需的行为。

答案 1 :(得分:0)

decimal.TryParse()方法将字符串作为其第一个参数,并尝试将其转换为decimal值。在您的情况下,传递给TryParse()的变量已经是decimal个变量,无需解析它们。如果您只想将类变量复制到局部变量,您需要做的就是:

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

或者在这种特殊情况下,您可以直接将类变量传递给AddOurNumbers

DataTier.AddOurNumbers(firstCounter, secondCounter, finalCounter);

这里需要注意的一点是,在将C#中的值decimals和其他基元从一个值分配给另一个值时,它们会被复制,或者将它们传递给方法。这意味着即使firstCountersecondCounterthirdCounter的值在调用DataTier.AddOurNumbers()后发生更改,您的数据层已经收到的值也不会更改。