本文介绍了无效的强制转换异常(从数字转换时,必须小于无穷大)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个有两列的表:step,z-value如下: step | z-value 1 | 1 2 | 4 3 | 9 4 | 16 i我试图遍历各行该表用于获取z值列中的值,以便执行计算以插入新单元格。 这样的事情: step | z-value | z ^ 3(这是新列但要附加到不同的表格) 1 | 1 | 1 2 | 4 | 16 3 | 9 | 81 4 | 16 | 256 由于某种原因,我没有抓住单元格中的值,我不确定如何继续。 感谢您的帮助。i have a table with two columns: step, z-value like this:step|z-value1|12|43|94|16i am trying to loop through the rows of the table to grab the value in the z-value column in order to perform a calculation to insert into a new cell.something like this:step|z-value|z^3(this is the new column but to be attached to a DIFFERENT table)1|1|12|4|163|9|814|16|256for some reason, i am not grabbing the value in the cell and I'm unsure how to proceed.thank you for any help.public DataTable zone2(inputParametersBO ipBO){ //imports original table DataTable preAngle = paBCBLL.z2BCPA(ipBO); //creates new table for calculations to go into DataTable dt = new DataTable(); DataColumn c1 = new DataColumn("RotatedX^2"); DataColumn c2 = new DataColumn("RotatedX"); DataColumn c3 = new DataColumn("RotatedY"); c1.DataType = System.Type.GetType("System.Decimal"); c2.DataType = System.Type.GetType("System.Decimal"); c3.DataType = System.Type.GetType("System.Decimal"); dt.Columns.Add(c1); dt.Columns.Add(c2); dt.Columns.Add(c3); foreach (DataRow row in preAngle.Rows) { double zVal = (double)row["Z2_BC"];//gets value from col2 of orig table double xStep = (double)row["step"];//gets value from col1 of orig table double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS)); double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS)); double rotatedXS = Math.Pow(rotatedX,2); DataRow dr = dt.NewRow(); dr["RotatedX^2"] = rotatedXS; dr["RotatedX"] = rotatedX; dr["RotatedY"] = rotatedY; dt.Rows.Add(dr); } return dt;}推荐答案dr["RotatedX^2"] = (decimal)rotatedXS; 我还会做些什么我建议的其他更改 - 使用类型安全的数据访问方法可以帮助更早地捕获此类错误。I'd still make some of the other changes I suggested - using type-safe data access methods can help catch such errors earlier.double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS)); double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS)); 当xStep = ipBO.z2bcxS时,根本原因可能是除以零。 为什么不用try catch块添加一些代码,看看是否发现除零异常。The root cause may be due to divide by zero when xStep = ipBO.z2bcxS.Why don't you add some code with try catch block and see if you see any divide by zero exception occurs. 这篇关于无效的强制转换异常(从数字转换时,必须小于无穷大)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 04:57