如何减少在C#这IF-ELSE阶梯 [英] How to reduce this IF-Else ladder in c#

查看:1371
本文介绍了如何减少在C#这IF-ELSE阶梯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所创建的第一个集中控制可见对我的form.According的要求,任何控制可以被隐藏在form.So我必须找到第一个可见的控制和聚焦它的中频-Else阶梯。

This is the IF -Else ladder which I have created to focus first visible control on my form.According to the requirement any control can be hidden on the form.So i had to find first visible control and focus it.

 if (ddlTranscriptionMethod.Visible)
    {
        ddlTranscriptionMethod.Focus();
    }
    else if (ddlSpeechRecognition.Visible)
    {
        ddlSpeechRecognition.Focus();
    }
    else if (!SliderControl1.SliderDisable)
    {
        SliderControl1.Focus();
    }
    else if (ddlESignature.Visible)
    {
        ddlESignature.Focus();
    }
    else
    {
        if (tblDistributionMethods.Visible)
        {
            if (chkViaFax.Visible)
            {
                chkViaFax.Focus();
            }
            else if (chkViaInterface.Visible)
            {
                chkViaInterface.Focus();
            }
            else if (chkViaPrint.Visible)
            {
                chkViaPrint.Focus();
            }
            else
            {
                chkViaSelfService.Focus();
            }
        }
    }

是否有这样做的任何其他方式。我想使用LINQ将抢去的性能,我必须tranverse整个页面集合。我深有masterpages.Please提示页面上。

Is there any other way of doing this. I thought using LINQ will hog the performance as i have to tranverse the whole page collection. I am deep on page which has masterpages.Please suggest.

推荐答案

我觉得你的树是好的。这当然的看起来的一样,可以简化逻辑树,你的嗅觉感好是怀疑它。然而,这似乎是该逻辑树反映了你所需要的。在的逻辑真的是这样错综复杂,这是有条件的框架,C#给你来处理这种情况。我不认为它可以改进。

I think your tree is good. This certainly looks like a logic tree that can be simplified, and you have a good sense of smell to be suspicious of it. However, it seems to be that the logic tree reflects what you need. The logic really is this convoluted, and this is the conditional framework that C# gives you to handle this situation. I don't think it can be improved.

如果你有控件一个简单的列表,应该有重点,你想给重点,第一个可见控制列表,你可以这样做:

If you had a simple list of controls that should have the focus, and you wanted to give focus to the first visible control in the list, you could do this:

(From c in ListOfControls
Where c.visible = true
Select c).First.Focus();

不过,看来你还有其他的一些标准,这样,才不会工作。

But, it appears you have some additional criteria, so that wouldn't work.

这篇关于如何减少在C#这IF-ELSE阶梯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆