需要了解此代码用于图表

时间:2019-06-14 13:44:10

标签: c#

bool metinoysonuc = Double.TryParse(txt_metin.Text, out metinoy);
bool oktayoysonuc = Double.TryParse(txt_oktay.Text, out oktayoy);
bool fatihoysonuc = Double.TryParse(txt_fatih.Text, out fatihoy);
bool terimoysonuc = Double.TryParse(txt_terim.Text, out terimoy);

这行在做什么?结果被写入布尔变量,但未使用(我认为)。如果使用它,您可以解释如何做。

// Full Code
double metinoy, oktayoy, fatihoy, terimoy;
bool metinoysonuc = Double.TryParse(txt_metin.Text, out metinoy);
bool oktayoysonuc = Double.TryParse(txt_oktay.Text, out oktayoy);
bool fatihoysonuc = Double.TryParse(txt_fatih.Text, out fatihoy);
bool terimoysonuc = Double.TryParse(txt_terim.Text, out terimoy);
// her atama öncesi grafik temizleme
foreach (var series in chart1.Series)
{
  series.Points.Clear();
}
chart1.Series["Notlar"].Points.Add(metinoy);
chart1.Series["Notlar"].Points.Add(oktayoy);
chart1.Series["Notlar"].Points.Add(fatihoy);
chart1.Series["Notlar"].Points.Add(terimoy);

chart1.Series["Notlar"].Points[0].AxisLabel = lbl_metin.Text;
chart1.Series["Notlar"].Points[1].AxisLabel = lbl_oktay.Text;
chart1.Series["Notlar"].Points[2].AxisLabel = lbl_fatih.Text;
chart1.Series["Notlar"].Points[3].AxisLabel = lbl_terim.Text;
chart1.Series["Notlar"].Color = Color.Yellow;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 70;
chart1.BackColor = Color.DarkRed;

2 个答案:

答案 0 :(得分:3)

确实没有使用bool变量(例如metinoysonuc)。因此,您可以“安全地”删除它们(“安全”意味着代码仍将编译并执行):

Double.TryParse(txt_metin.Text, out metinoy);
Double.TryParse(txt_oktay.Text, out oktayoy);
Double.TryParse(txt_fatih.Text, out fatihoy);
Double.TryParse(txt_terim.Text, out terimoy);

这些相同的操作仍会在变量metinoy中产生已解析的结果。

但是,真正的问题是...您是否想对这些bool结果做任何事情?这些值指示解析操作是成功还是失败。如果解析操作失败,您是否要使代码以0的值静默继续进行,还是要响应该错误?

例如:

bool metinoysonuc = Double.TryParse(txt_metin.Text, out metinoy);
bool oktayoysonuc = Double.TryParse(txt_oktay.Text, out oktayoy);
bool fatihoysonuc = Double.TryParse(txt_fatih.Text, out fatihoy);
bool terimoysonuc = Double.TryParse(txt_terim.Text, out terimoy);

if (!metinoysonuc || !oktayoysonuc || !fatihoysonuc || !terimoysonuc)
{
    // At least one parse operation failed.  Notify the user?
}

您要如何处理解析失败取决于您。 bool值仅表示解析操作是否成功。

答案 1 :(得分:0)

关于metinoysonuc = Double.TryParse(txt_metin.Text, out metinoy);

如果txt_metin.Text中的字符串不是有效的double,则Double.TryParse将返回false,这也将metinoysonuc设置为{{1} }和false将是zero

如果metinoy中的字符串是有效的txt_metin.Text,则double将返回Double.TryParse,这也将true设置为metinoysonuc并且true将包含metinoy中包含的已解析的double

虽然看起来不像您的代码段中使用了布尔值,但是如果txt_metin.Textmetinoysonuc,则在false为{{3 }}。