【问题标题】:Shortening this if condition缩短这个 if 条件
【发布时间】:2012-02-03 01:09:29
【问题描述】:

几天前我问了一个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 的值来确定它必须更改哪个超链接按钮背景来缩短这种情况?

【问题讨论】:

    标签: c#-4.0 silverlight-4.0


    【解决方案1】:

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

    var buttons = new [] {
        hyperlinkButton1,
        hyperlinkButton2,
        hyperlinkButton3,
        hyperlinkButton4,
        hyperlinkButton5,
        hyperlinkButton6,
        hyperlinkButton7,
        hyperlinkButton8,
        hyperlinkButton9,
        // ...
    }
    
    //.... 
    
    buttons[DateTime.Today.Day-1].Background=new SolidColorBrush(Colors.Black);
    

    【讨论】:

      【解决方案2】:

      在一般级别上,基于数组的方法是多个 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);
      }
      

      【讨论】:

      • 感谢您的帮助,这真的让我的生活更轻松;)
      【解决方案3】:
      switch(d)
      {
      case 1: doThings(); break;
      case 2: doThings2(); break;
      case 3:
          doSomeThings();
          doMoreThings();
          break;
      default:
          runThingsIfDIsNotListed();
          break;
      }
      

      等等

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 2014-05-07
        • 2019-12-01
        • 2020-11-17
        • 1970-01-01
        • 2015-05-19
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多