C#-高效地编写代码
我正在下面粘贴我的代码片段。有没有人能提出一个更好更有效的写作方法。我想最低限度的代码编写,避免重复C#-高效地编写代码,c#,C#,我正在下面粘贴我的代码片段。有没有人能提出一个更好更有效的写作方法。我想最低限度的代码编写,避免重复 private void SetControlVisibility() { if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicById") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsByIds")) { Label2.Tex
private void SetControlVisibility()
{
if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicById") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsByIds"))
{
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicBySedols") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsBySedols"))
{
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds"))
{
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
}
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol"))
{
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
}
}
带开关的备用代码:
private void SetControlVisibility()
{
if (DropDownList1.SelectedItem != null)
{
switch (DropDownList1.SelectedItem.Text)
{
case "GetAssetsBasicById":
case "GetAssetDetailsByIds":
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
break;
case "GetAssetsBasicBySedols":
case "GetAssetDetailsBySedols":
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
chkExcludeMAPFunds.Visible = false;
chkPublishXML.Visible = true;
break;
case "GetInvestmentReportByIds":
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
break;
case "GetInvestmentReportBySedol":
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = true;
chkPublishXML.Visible = false;
break;
default:
// we do it wrong :(
throw new NotSupportedException();
}
}
}
另一种解决方案是将项的
标记属性与预定义的枚举值一起使用。首先,将CHK从if
中取出,并使用?:
操作符将它们的符号简化
然后,由于每个if
-else if
中只有一条语句,因此可以删除括号
if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicById") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsByIds"))
Label2.Text = "(Please enter asset ids for e.g. 1,2)";
else if (DropDownList1.SelectedItem.Text.Equals("GetAssetsBasicBySedols") || DropDownList1.SelectedItem.Text.Equals("GetAssetDetailsBySedols"))
Label2.Text = "(Please enter sedols for e.g. B1YW440,0003496)";
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds"))
Label2.Text = "(Please enter asset ids for e.g. 1:100)";
else if (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol"))
Label2.Text = "(Please enter sedols for e.g. B1YW440:100)";
chkExcludeMAPFunds.Visible = (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds") || DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol") ? true : false;
chkPublishXML.Visible = (DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportByIds") || DropDownList1.SelectedItem.Text.Equals("GetInvestmentReportBySedol") ? false : true;
这样,我们就省去了许多行。我的备用代码:
private void SetControlVisibility()
{
string resultText;
bool b = false;
switch (DropDownList1.SelectedItem.Text)
{
case "GetAssetsBasicById":
case "GetAssetDetailsByIds":
b = true;
resultText = "(Please enter asset ids for e.g. 1,2)";
break;
case "GetAssetsBasicBySedols":
case "GetAssetDetailsBySedols":
b = true;
resultText = "(Please enter sedols for e.g. B1YW440,0003496)";
break;
case "GetInvestmentReportByIds":
resultText = "(Please enter asset ids for e.g. 1:100)";
break;
case "GetInvestmentReportBySedol":
resultText = "(Please enter sedols for e.g. B1YW440:100)";
break;
default: return;
}
chkExcludeMAPFunds.Visible = !b;
chkPublishXML.Visible = b;
Label2.Text = resultText;
}
您可以使用字典来避免嵌套的if和switch/case:
private readonly Dictionary<string, Tuple<string, bool, bool>> _dropDownMap = new Dictionary<string, Tuple<string, bool, bool>>
{
{"GetAssetsBasicById", new Tuple<string, bool, bool>("(Please enter asset ids for e.g. 1,2)", false, true) },
{"GetAssetDetailsByIds", new Tuple<string, bool, bool>("(Please enter asset ids for e.g. 1,2)", false, true) },
...
};
private void SetControlVisibility()
{
var mapping = _dropDownMap[DropDownList1.SelectedItem.Text];
if (mapping != null)
{
Label2.Text = mapping.Item1;
chkExcludeMAPFunds.Visible = mapping.Item2;
chkPublishXML.Visible = mapping.Item3;
}
}
尝试切换(DropDownList1.SelectedItem.Text)
如上所述,但将DropDownList1.SelectedItem.Text存储到一个字符串中,并检查其是否为null,然后再切换nice and clean,+1(对于默认注释,引发了一个微笑!)我仍然感觉在设置视图可见性方面存在代码重复checkboxes@Tom所以从这篇文章中汲取灵感,并通过自己添加的内容进一步完善它?@Tom,正如我所写的,另一个解决方案是使用Tag,但我不知道如何创建下拉项。你能发布下拉列表初始化的代码吗?我会将上面的代码提取到另一个方法中以保持它的干净。我更喜欢添加一个func,使它与null一起实现得更干净check@Karan,但需要小心使用,因为Item
,Item2
等会降低可读性。在这种情况下,这可能是正常的,因为代码小到足以将元组的字段与数据关联起来。不过,在更大的解决方案中,显式数据类型会有所帮助。由于这里的元组示例可能更好,我将这两个示例交换了一下。我同意你的观点。为什么我两次被否决?至少知道我的编码风格有什么问题是很好的@杜默将军
private class DropDownMappings
{
public DropDownMappings(label, excludeMAPFundsVisible, publishXMLVisible)
{
Label2Text = label;
ExcludeMAPFundsVisible = excludeMAPFundsVisible;
PublishXMLVisible = publishXMLVisible;
}
public string Label2Text { get; set; }
public bool ExcludeMAPFundsVisible { get; set; }
public bool PublishXMLVisible { get; set; }
}
private readonly Dictionary<string, DropDownMappings> _dropDownMap = new Dictionary<string, DropDownMappings>
{
{"GetAssetsBasicById", new DropDownMappings("(Please enter asset ids for e.g. 1,2)", false, true) },
{"GetAssetDetailsByIds", new DropDownMappings("(Please enter asset ids for e.g. 1,2)", false, true) },
...
};
private void SetControlVisibility()
{
var mapping = _dropDownMap[DropDownList1.SelectedItem.Text];
if (mapping != null)
{
Label2.Text = mapping.Label2Text;
chkExcludeMAPFunds.Visible = mapping.ExcludeMAPFundsVisible;
chkPublishXML.Visible = mapping.PublishXMLVisible;
}
}