如何消除可空的bool的使用

时间:2012-05-16 16:11:29

标签: c# boolean nullable

我想出了这个代码,我认为这个代码相当聪明(要求是如果所选日期是过去的,TextBoxes应该是readonly,否则(今天的日期或将来的日期)它们应该是可编辑的) :

bool? setReadOnly = null;

if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
    setReadOnly = true;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
    setReadOnly = false;
}
if (setReadOnlyToTrue.HasValue) {
    foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
        if (ctrl is TextBox) {
            tb = (TextBox)ctrl;
            tb.ReadOnly = setReadOnlyToTrue.Value;
        }
    }
}

...但在我的竞争对手中发现可空的bool是“数据类型不受欢迎”。

是否有一种非复杂的方法来做同样的事情(如果需要更改只读值,则只循环控制?)。当然,我可以简单地设置它们,无论它们是否需要这样设置:

if (SelectedDateIsInThePast()) {
    setReadOnly = true;
} else {
    setReadOnly = false;
}
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
    if (ctrl is TextBox) {
        tb = (TextBox)ctrl;
        tb.ReadOnly = setReadOnly;
    }
}

......但如果能够合理地避免它们,我不喜欢进行模拟操作。

5 个答案:

答案 0 :(得分:4)

将循环视为一个方法,并且仅在您设置setReadOnly的情况下调用该方法:

if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
    SetReadOnly(true);
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
    SetReadOnly(false);
}

答案 1 :(得分:1)

您可以使用|=&=和两个不可空的布尔值来实现相同的要求:

bool forceReadOnly = SelectedDateIsInThePast() && (!currentlyReadOnly);
bool clearReadOnly = !(!SelectedDateIsInThePast() && (currentlyReadOnly));

foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
    if (ctrl is TextBox) {
        tb = (TextBox)ctrl;
        tb.ReadOnly |= forceReadOnly;
        tb.ReadOnly &= clearReadOnly;
    }
}

答案 2 :(得分:1)

我认为可以自然的bool很好......但另一种方式是:

public enum ControlState
{
    Unknown = 0,
    DateInPast,
    DateInFuture
}

....

var state = ControlState.Unknown;

if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
    state = ControlState.DateInPast;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
    state = ControlState.DateInFuture;
}
if (state != ControlState.Unknown) {
    foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
        if (ctrl is TextBox) {
            tb = (TextBox)ctrl;
            tb.ReadOnly = setReadOnlyToTrue.Value;
        }
    }
}

答案 3 :(得分:1)

使用具有三个独立,有意义状态的枚举?

enum ShouldSetState
{
    No, 
    SetReadOnly,
    SetReadable
}

然后做

ShouldSetState setState = ShouldSetState.No;

if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
    setState = ShouldSetState.SetReadOnly;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
    setState = ShouldSetState.SetReadable;
}
if (setState != ShouldSetState.No) {
    foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
        if (ctrl is TextBox) {
            tb = (TextBox)ctrl;
            tb.ReadOnly = setState == ShouldSetState.SetReadOnly;
        }
    }
}

答案 4 :(得分:0)

如果不使用可空的bool,您的代码可以更加简洁:

bool inThePast = SelectedDateIsInThePast();
if (currentlyReadOnly != inThePast )
{
    currentlyReadOnly = inThePast;
    foreach(var tb in tableLayoutPanelPlatypus.Controls.OfType<TextBox>())
        tb.ReadOnly = currentlyReadOnly;
}

此外,如果您必须执行大量此类UI操作,则可以考虑使用data binding