编写C#类 [英] Writting C# classes
本文介绍了编写C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,有人可以告诉我这段代码有什么问题吗,它给了我编译错误.我有条件分配的变量的使用未分配的局部变量".
Pls guys, can someone tell me whats wrong with this code, it give me compile error; "Use of unassigned local variable" for the variables i conditionally assigned.
internal string reinforcement(int[,] a, int b, double c, double d, double e, char f) //market, dia, breath, depth, As, steel type
{
double As_min = 0.0, As_max;
int g;
string type;
As_max = 4 * c * d;
if (f == 'H')
{
As_min = 0.13 * c * d;
type = "Y";
}
else if (f == 'M')
{
As_min = 0.24 * c * d;
type = "R";
}
switch (b)
{
case 8: g = 0; break;
case 10: g = 1; break;
case 12: g = 2; break;
case 16: g = 3; break;
case 20: g = 4; break;
case 25: g = 5; break;
case 32: g = 6; break;
case 40: g = 7; break;
default: g = 0; break;
}
int h, m;
if (e > As_min && e < As_max)
{
for (int j = g; j < 8; j++)
{
for (int k = 2; k < 9; k++)
{
if (e >= a[j, k])
continue;
else
{
h = k; // no. of reinforcements
m = j; // diameter of reinforcements
goto end;
}
}
}
}
else if (e < As_min)
{
for (int j = g; j < 8; j++)
{
for (int k = 2; k < 9; k++)
{
if (e >= a[j, k])
continue;
else
{
h = k; // no. of reinforcements
m = j; // diameter of reinforcements
goto end;
}
}
}
}
end: ;
int n;
switch (m)
{
case 0: n = 8; break;
case 1: n = 10; break;
case 2: n = 12; break;
case 3: n = 16; break;
case 4: n = 20; break;
case 5: n = 25; break;
case 6: n = 32; break;
case 7: n = 40; break;
}
string provide = "";
if (e == 0)
provide = "";
else
provide = "\t\tProvide " + h + "" + type + n + "(" + a[m, h] + "mm^2)";
return provide;
}
推荐答案
(很正确)发现type
可能永远不会得到值:
It has (quite rightly) spotted thattype
may never get a value:
string type;
As_max = 4 * c * d;
if (f == 'H')
{
As_min = 0.13 * c * d;
type = "Y";
}
else if (f == 'M')
{
As_min = 0.24 * c * d;
type = "R";
}
如果f
既不是"H"也不是"M"会发生什么?可能在您的特定应用程序中,reinforcement
将永远不会以f
不等于另一个的方式被调用-但是编译器不知道这一点!
[edit] Fat finger syndrome ...-OriginalGriff [/edit]
What happens if f
is neither ''H'' or ''M''? Probably, in your specific application, reinforcement
will never be called with f
not equal to one or the other - but the compiler doesn''t know that!
[edit]Fat fingers syndrome... - OriginalGriff[/edit]
避免此类错误的最佳方法是在定义时为所有变量分配默认值,就像您所做的那样.As_min
.
您的问题可能出在h
和m
上,因为它们可以被使用而无需分配值.
注意:为变量赋予有意义的名称将使您的代码更易于阅读.
The best way to avoid errors like this is to assign a default value to all variables when defined, like you did withAs_min
.
Your problem is probably withh
andm
as they can be used without getting a value assigned.
Note: Giving variables meaningful names will make your code more easy to read.
这篇关于编写C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文