缩短此 if 条件

发布于 2025-01-02 04:57:50 字数 2969 浏览 4 评论 0原文

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

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< 的值确定必须更改哪个超链接按钮背景来缩短此条件/代码>?

A few days ago I asked a question that might be a little unclear. Now I have written code that might illustrate the question better. Please look at the code below first:

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);
}

My question (as a beginner) is this: is there any way in C# to shorten this condition by making the application determine which hyperlinkbutton background it has to change depending on the value of d?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2025-01-09 04:57:50

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

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

//.... 

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

Define an array of the relevant controls, and use the integer to key into the array, remembering that arrays are 0-based and not 1-based.

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

//.... 

buttons[DateTime.Today.Day-1].Background=new SolidColorBrush(Colors.Black);
雄赳赳气昂昂 2025-01-09 04:57:50

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

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

The array based approach is a good alternative at a general level to the multiple if statements but since this is also tagged as Silverlight, you might be interested in taking advantage of the FrameworkElement.FindName Method if you can rely on the convention of naming the HyperlinkButtons with a common prefix.

var hyperlinkButton = this.FindName("hyperlinkButton" + DateTime.Now.Day) as HyperlinkButton;
if (hyperlinkButton != null)
{
    hyperlinkButton.Background = new SolidColorBrush(Colors.Black);
}
假扮的天使 2025-01-09 04:57:50
switch(d)
{
case 1: doThings(); break;
case 2: doThings2(); break;
case 3:
    doSomeThings();
    doMoreThings();
    break;
default:
    runThingsIfDIsNotListed();
    break;
}

ETC。

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

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文