传递信息有困难
本文关键字:信息 | 更新日期: 2023-09-27 17:54:42
我在为学校做的一个程序中似乎遇到了一点障碍。我想做一个新的类来接收数据,并得到一个总发送到一个新的屏幕。我做了和课堂上一样的事情,但不同的是,那是一个文本框而这次我需要移动的数据没有文本框,所以我尝试的方式给了我一个错误。以下是我目前得到的结果:
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部分,我不知道为什么它不工作,除了错误说"不能从十进制转换为字符串"。
改为
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
,不需要任何转换。
作为旁注,代码
decimal.TryParse(someString, out someOutputDecimal);
如果周围没有合适的if语句,它将静默失败(它不会通知任何关于失败的信息)。实际上,输出值被设置为0,它就像没有接收到错误输入一样。如果输入应该始终有效,则应该使用decimal.Parse(someString)
代替。然而,在某些情况下,如果输入无效,则默认为0,这可能是期望的行为。
decimal.TryParse()
方法将字符串作为其第一个参数,并尝试将其转换为decimal
值。在您的例子中,传递给TryParse()
的变量已经是decimal
变量,不需要解析它们。如果你只想把类变量复制到局部变量,你所需要做的就是:
firstResult = firstCounter;
secondResult = secondCounter;
finalResult = finalCounter;
或者在这种特殊情况下,您可以直接将类变量传递给AddOurNumbers
:
DataTier.AddOurNumbers(firstCounter, secondCounter, finalCounter);
这里需要注意的一点是,值类型,如decimals
和c#中的其他原语,在您将它们从一个值分配给另一个值或将它们传递给方法时都会被复制。这意味着即使firstCounter
、secondCounter
或thirdCounter
的值在调用DataTier.AddOurNumbers()
之后发生了变化,您的数据层已经接收到的值也不会改变。