我正在开发一个asp.net项目,它从前端获取值,将它们作为字符串存储在矩阵中。 当矩阵大小大于5 * 5 时,它会保持返回InvalidCastException(它在5 * 5矩阵及其下工作正常)。
下面附有代码,屏幕截图为Exception:
public partial class WebForm1 : System.Web.UI.Page
{
public DataSet relmatrix = new DataSet();
public DataTable rt = new DataTable();
public Double[] inconsistency;
public int nodersnum;
public string strrel;
protected void Button1_Click(object sender, EventArgs e)
{
nodersnum = Convert.ToInt16(count.Text);
switch (nodersnum)
{
case 1:
break;
case 2:
{ strrel = RelationAB2.Text; }
break;
case 3:
{ strrel = RelationAB3.Text + " " + RelationAC3.Text + " " + RelationBC3.Text; }
break;
case 4:
{ strrel = RelationAB4.Text + " " + RelationAC4.Text + " " + RelationAD4.Text + " " + RelationBC4.Text + " " + RelationBD4.Text + " " + RelationCD4.Text; }
break;
case 5:
{ strrel = RelationAB5.Text + " " + RelationAC5.Text + " " + RelationAD5.Text + " " + RelationAE5.Text + " " + RelationBC5.Text + " " + RelationBD5.Text + " " + RelationBE5.Text + " " + RelationCD5.Text + " " + RelationCE5.Text + " " + RelationDE5.Text; }
break;
case 6:
{ strrel = RelationAB6.Text + " " + RelationAC6.Text + " " + RelationAD6.Text + " " + RelationAE6.Text + " " + RelationAF6.Text + " " + RelationBC6.Text + " " + RelationBD6.Text + " " + RelationBE6.Text + " " + RelationBF6.Text + " " + RelationCD6.Text + " " + RelationCE6.Text + " " + RelationCF6.Text + " " + RelationDE6.Text + " " + RelationDF6.Text + " " + RelationEF6.Text; }
break;
case 7:
{ strrel = RelationAB7.Text + " " + RelationAC7.Text + " " + RelationAD7.Text + " " + RelationAE7.Text + " " + RelationAF7.Text + " " + RelationAG7.Text + " " + RelationBC7.Text + " " + RelationBD7.Text + " " + RelationBE7.Text + " " + RelationBF7.Text + " " + RelationBG7.Text + " " + RelationCD7.Text + " " + RelationCE7.Text + " " + RelationCF7.Text + " " + RelationCG7.Text + " " + RelationDE7.Text + " " + RelationDF7.Text + " " + RelationDG7.Text + " " + RelationEF7.Text + " " + RelationEG7.Text + " " + RelationFG7.Text; }
break;
default:
{ strrel = ""; }
break;
}
GenerateTable.generatetable(relmatrix, strrel, rt, nodersnum);
Class1.generatePC(nodersnum, relmatrix);
int num = 0;
double maxincon = 0.0;
int mi = 0, mj = 0, mk = 0;
inconsistency = new Double[43] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
//PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
//Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
Class1.sort(inconsistency);//sort inconsistency array
while (inconsistency[0] > 0.3333333)
{
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
// PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
if (inconsistency[0] == (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a))))//calculate the inconsistency value and store in the inconsistency array
{
if ((b * c) < a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + System.Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b + (b * x1) / (a + b + c));
c = (float)(c + (c * x1) / (a + b + c));
a = (float)(a - (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b + (b * x2) / (a + b + c));
c = (float)(c + (c * x2) / (a + b + c));
a = (float)(a - (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b + (b * x) / (a + b + c));
c = (float)(c + (c * x) / (a + b + c));
a = (float)(a - (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
else if ((b * c) > a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = -1 * (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b - (b * x1) / (a + b + c));
c = (float)(c - (c * x1) / (a + b + c));
a = (float)(a + (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b - (b * x2) / (a + b + c));
c = (float)(c - (c * x2) / (a + b + c));
a = (float)(a + (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b - (b * x) / (a + b + c));
c = (float)(c - (c * x) / (a + b + c));
a = (float)(a + (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
}
relmatrix.Tables["relTable"].Rows[i][j] = Convert.ToString(a);
relmatrix.Tables["relTable"].Rows[i][k] = Convert.ToString(b);
relmatrix.Tables["relTable"].Rows[k][j] = Convert.ToString(c);
}
}
}
}
num = 0;
maxincon = 0.0;
mi = 0;
mj = 0;
mk = 0;
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
// Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
//sort inconsistency array
Class1.sort(inconsistency);
}
//Fill up the whole pairwise comparsion matrix, when row=col, the value =1, when row<col the vaule[col][row] is "1/"[col][row]
//nodersnum is how many nodes, row is the matrix row, col is column of matrix
for (int row = 0; row < nodersnum; row++)
{
for (int col = row; col < nodersnum; col++)
{
if (row < col)
{
//set the the value of lower matrix
relmatrix.Tables["relTable"].Rows[col][row] = "1/" + relmatrix.Tables["relTable"].Rows[row][col];
}
//set the value of diagnoal
else if (row == col)
{
relmatrix.Tables["relTable"].Rows[row][col] = "1";
}
}
}
//compute the weight of each element
Double[] rowproduct;
rowproduct = new Double[7] { 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < nodersnum; i++)
{
for (int j = 0; j < nodersnum; j++)
{
if (i >= j)
{
rowproduct[i] = rowproduct[i] / Convert.ToDouble(relmatrix.Tables["relTable"].Rows[j][i]);
}
else
{
rowproduct[i] = rowproduct[i] * Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);
}
}
}
Double[] num1;
num1 = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
double numsum = 0;
//compute each row total number(power of node number)
for (int i = 0; i < nodersnum; i++)
{
num1[i] = Math.Pow(rowproduct[i], 1 / (double)nodersnum);
numsum = numsum + num1[i];
}
//transfer into the number of percentage
Double[] weight;
weight = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum; i++)
{
weight[i] = (int)Math.Round(100 * num1[i] / numsum * 100) / 100f;
Console.WriteLine(weight[i]);
}
GridView2.DataSource = relmatrix;
GridView2.DataBind();
this.GridView2.Rows[mi].Cells[mj].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mi].Cells[mk].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mk].Cells[mj].BackColor = System.Drawing.Color.Red;
Label3.Text = "Maximum Inconsistency after Reduction: " + Convert.ToString(inconsistency[0]);
TextBox1.Text = Convert.ToString(weight[0]);
TextBox2.Text = Convert.ToString(weight[1]);
TextBox3.Text = Convert.ToString(weight[2]);
TextBox4.Text = Convert.ToString(weight[3]);
TextBox5.Text = Convert.ToString(weight[4]);
TextBox6.Text = Convert.ToString(weight[5]);
TextBox7.Text = Convert.ToString(weight[6]);
}
}
答案 0 :(得分:1)
看起来您的矩阵包含空值。你不能把它们变成Double。
像这样检查:
if (relmatrix.Tables["relTable"].Rows[i][k] != DBNull.Value)
或者
if (relmatrix.Tables["relTable"].Rows[i][k] != null)
答案 1 :(得分:0)
使用nullable double,如下所示:
var mynull = DBNull.Value;
Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);
在你的情况下:
Double? c = Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) ? (double?)null : Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);;
或者使用if-else:
if(Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) == true)
{
Double? c = (double?)null;
}
else
{
Double? c = Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);
}