缩短这个if条件

时间:2012-02-03 01:09:29

标签: c#-4.0 silverlight-4.0

前几天我问question可能有点不清楚。现在我编写了可以更好地说明问题的代码。请先查看下面的代码:

int d;
d = DateTime.Today.Day;
if (d==1)
{
    hyperlinkButton1.Background=new SolidColorBrush(Colors.Black);
}
else if (d==2)
{
    hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==3)
{
    hyperlinkButton3.Background=new SolidColorBrush(Colors.Black);
}
else if (d==4)
{
    hyperlinkButton4.Background=new SolidColorBrush(Colors.Black);
}
else if (d==5)
{
    hyperlinkButton5.Background=new SolidColorBrush(Colors.Black);
}
else if (d==6)
{
    hyperlinkButton6.Background=new SolidColorBrush(Colors.Black);
}
else if (d==7)
{
    hyperlinkButton7.Background=new SolidColorBrush(Colors.Black);
}
else if (d==8)
{
    hyperlinkButton8.Background=new SolidColorBrush(Colors.Black);
}
else if (d==9)
{
    hyperlinkButton9.Background=new SolidColorBrush(Colors.Black);
}
else if (d==10)
{
    hyperlinkButton10.Background=new SolidColorBrush(Colors.Black);
}
else if (d==11)
{
    hyperlinkButton11.Background=new SolidColorBrush(Colors.Black);
}
else if (d==12)
{
    hyperlinkButton12.Background=new SolidColorBrush(Colors.Black);
}
else if (d==13)
{
    hyperlinkButton13.Background=new SolidColorBrush(Colors.Black);
}
else if (d==14)
{
    hyperlinkButton14.Background=new SolidColorBrush(Colors.Black);
}
else if (d==15)
{
    hyperlinkButton15.Background=new SolidColorBrush(Colors.Black);
}
else if (d==16)
{
    hyperlinkButton16.Background=new SolidColorBrush(Colors.Black);
}
else if (d==17)
{
    hyperlinkButton17.Background=new SolidColorBrush(Colors.Black);
}
else if (d==18)
{
    hyperlinkButton18.Background = new SolidColorBrush(Colors.Black);
}
else if (d==19)
{
    hyperlinkButton19.Background=new SolidColorBrush(Colors.Black);
}
else if (d==20)
{
    hyperlinkButton20.Background=new SolidColorBrush(Colors.Black);
}
else if (d==21)
{
    hyperlinkButton21.Background=new SolidColorBrush(Colors.Black);
}
else if (d==22)
{
    hyperlinkButton22.Background=new SolidColorBrush(Colors.Black);
}
else if (d==23)
{
    hyperlinkButton23.Background=new SolidColorBrush(Colors.Black);
}
else if (d==24)
{
    hyperlinkButton24.Background=new SolidColorBrush(Colors.Black);
}
else if (d==25)
{
    hyperlinkButton25.Background=new SolidColorBrush(Colors.Black);
}
else if (d==26)
{
    hyperlinkButton26.Background=new SolidColorBrush(Colors.Black);
}
else if (d==27)
{
    hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==28)
{
    hyperlinkButton28.Background=new SolidColorBrush(Colors.Black);
}
else if (d==29)
{
    hyperlinkButton29.Background=new SolidColorBrush(Colors.Black);
}
else if (d==30)
{
    hyperlinkButton30.Background=new SolidColorBrush(Colors.Black);
}
else
{ 
    hyperlinkButton31.Background=new SolidColorBrush(Colors.Black);
}

我的问题(作为初学者)是这样的:C#中有什么方法可以通过让应用程序根据d的值来确定它必须改变哪个超链接按钮背景来缩短这种情况吗?

3 个答案:

答案 0 :(得分:4)

定义相关控件的数组,并使用整数键入数组,记住数组是从0开始而不是从1开始。

var buttons = new [] {
    hyperlinkButton1,
    hyperlinkButton2,
    hyperlinkButton3,
    hyperlinkButton4,
    hyperlinkButton5,
    hyperlinkButton6,
    hyperlinkButton7,
    hyperlinkButton8,
    hyperlinkButton9,
    // ...
}

//.... 

buttons[DateTime.Today.Day-1].Background=new SolidColorBrush(Colors.Black);

答案 1 :(得分:3)

基于数组的方法在多个if语句的一般级别上是一个很好的选择,但由于它也被标记为Silverlight,如果您可以依赖约定,您可能有兴趣利用FrameworkElement.FindName Method使用公共前缀命名HyperlinkBut​​tons。

var hyperlinkButton = this.FindName("hyperlinkButton" + DateTime.Now.Day) as HyperlinkButton;
if (hyperlinkButton != null)
{
    hyperlinkButton.Background = new SolidColorBrush(Colors.Black);
}

答案 2 :(得分:0)

switch(d)
{
case 1: doThings(); break;
case 2: doThings2(); break;
case 3:
    doSomeThings();
    doMoreThings();
    break;
default:
    runThingsIfDIsNotListed();
    break;
}

相关问题