2013-01-19 75 views
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; 
    } 
} 

的错误是在三个decimal.TryParse部分,我不知道为什么它不工作,除了错误说“不能从十进制转换为字符串”。任何帮助将不胜感激。

回答

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,可能是所需的行为。

+0

谢谢,这固定了它。现在我可以完成它。 – Kimmy1235

0

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

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

或在这种特殊情况下,你可以通过类变量直接进入AddOurNumbers

DataTier.AddOurNumbers(firstCounter, secondCounter, finalCounter); 

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

+0

他们的不变性与他们被复制的原因无关。他们被复制,因为他们是[值类型](http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx)。即使一个可变结构(强烈推荐使用)也会在分配给不同变量时被复制。结果与在值类型中添加可变引用类型几乎完全相同。但是C#基元是值类型的特殊情况,它们的处理方式与大多数不同。然而,这里的信息与这个问题中的问题无关。 – Aidiakapi

+0

谢谢你的澄清。我不认为他们的价值类型在这里一定是不相关的,因为我怀疑提问者使用'TryParse()'试图复制这些变量。 – JLRishe

+0

对不起,迟到的回应。引用问题“*我做了同样的事情,我们在课堂上做了,但不同的是,这是从一个文本框,并没有我需要移动的数据文本框这一次*”。所以他不想复制它们,他只是使用他的类中的代码,它使用了一个“TextBox”(因此是一个“string”)作为输入。他只是试图将'decimal'转换为'decimal',这是多余的,并且由于函数签名导致错误。 – Aidiakapi